SQL CREATE TABLE statement is used to create a new table in the database. Here is the general form of the CREATE TABLE statement to create a new table to store some record in the table:
CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), .... );
Here table_name is the name of the table that you want to use. And column_name1, column_name2, column_name3, are the name of the column which are going to place inside the table table_name. And size parameter specifies, what type of data, column can hold
Now, let's take an example on SQL CREATE TABLE. Here, we are going to create a table named CUSTOMERS, containing these five columns:
Here is an example, creates the table CUSTOMERS making ID as primary key and NOT NULL are the constraints showing that these fields can't be NULL while creating the records in this table:
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Note - You can't create two or more than two tables with same name
After querying the above SQL statement, will create a new table in your database named CUSTOMERS. Here is the output displayed after querying the above SQL statement:
You can also verify if your table has been created successfully by looking at the message displayed by the SQL server, otherwise you can use the SQL DESC command as shown here:
After querying the above SQL statement, you will get the following output:
Now, you have a table named CUSTOMERS available in your database, you can use it to store required information inside it.
© Copyright 2021. All Rights Reserved.