SQL - Create Table
SQL - Create Table
In RDBMS, Database tables are used to store the data in the form of some structures
(fields and records). Here, a field is a column defining the type of data to be stored
in a table and record is a row containing actual data. In simple words, we can say a
Table is a combination of rows and columns.
SQL provides various queries to interact with the data in a convenient way. We can
use SQL statements to create and delete tables, inserting, updating and deleting
data in these tables.
For a more detail on different concepts related to RDBMS please check RDBMS
Concepts tutorial.
Syntax
https://www.tutorialspoint.com/sql/sql-create-table.htm 1/5
4/27/24, 3:16 PM SQL - CREATE Table
CREATE TABLE is the keyword telling the database system what you want to
do. In this case, you want to create a new table. The unique name or
identifier for the table follows the CREATE TABLE statement.
The column parameters (e.g. column1, column2, column3, etc.) specify the
names of the columns of the table.
The datatype parameter specifies the type of data the column can hold (e.g.
integer, varchar, string, etc.).
PRIMARY KEY constraint uniquely identifies each record in a table. Primary
keys must contain UNIQUE values, and cannot contain NULL values.
Verification
Once your table is created, you can check if it has been created successfully or not.
You can use SQL DESC table_name command to list down the description of the
table as follows:
DESC CUSTOMERS;
This will display the structure of the table created: column names, their respective
data types, constraints (if any) etc.
https://www.tutorialspoint.com/sql/sql-create-table.htm 2/5
4/27/24, 3:16 PM SQL - CREATE Table
Now, you have CUSTOMERS table available in your database which you can use to
store the required information related to customers.
So to avoid such error we can use SQL command CREATE TABLE IF NOT EXISTS
to create a table.
Syntax
The following SQL command will create the CUSTOMERS table only when there is no
table exists with the same name otherwise it will exit without any error.
https://www.tutorialspoint.com/sql/sql-create-table.htm 3/5
4/27/24, 3:16 PM SQL - CREATE Table
Syntax
The basic syntax for creating a table from another table is as follows −
Here, column1, column2... are the fields of the existing table and the same would be
used to create fields of the new table.
Following is an example, which would create a table SALARY using the CUSTOMERS
table and having the fields customer ID and customer SALARY −
This will create a new table SALARY which will have the following structure −
https://www.tutorialspoint.com/sql/sql-create-table.htm 4/5
4/27/24, 3:16 PM SQL - CREATE Table
https://www.tutorialspoint.com/sql/sql-create-table.htm 5/5