Create Table - SQL

To create a table in SQL, you need to use the CREATE TABLE statement. The CREATE TABLE statement allows you to define the structure of the table by specifying the column names, data types, constraints, and other attributes. Here's an example of the CREATE TABLE statement:

Example
CREATE TABLE table_name (
  column1 datatype constraint,
  column2 datatype constraint,
  column3 datatype constraint,
  ...
);

Let's break down the components of the CREATE TABLE statement:

  • CREATE TABLE: This is the keyword used to indicate that you want to create a new table.
  • table_name: This is the name you choose for your table. Choose a meaningful and descriptive name.
  • column1, column2, column3, ...: These are the names of the columns in the table.
  • datatype: This specifies the data type of the corresponding column.
  • constraint: This specifies any constraints or rules applied to the column, such as primary key, foreign key, not null, unique, etc.

Here's an example that creates a table named "employees" with a few columns:

Example
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  age INT,
  salary DECIMAL(10, 2)
);

In this example, the table "employees" is created with columns: "id" (integer type and primary key constraint), "name" (variable-length character type with a maximum length of 50 and not null constraint), "age" (integer type), and "salary" (decimal type with precision 10 and scale 2).

Note that the specific data types and constraints available may vary depending on the database management system (DBMS) you are using. It's important to refer to the documentation or specific syntax of your DBMS to ensure the correct creation of tables.