SQL CREATE TABLE
In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE command.
While creating a table, we need to specify a table name, a column name, and a data type for each column where data gets stored and column size.
Let’s learn how to use CREATE TABLE command to create a table.
The syntax of the CREATE TABLE is as follows:
CREATE TABLE TABLE-NAME (COLUMNNAME_1 DATATYPE(SIZE), COLUMNNAME_2 DATATYPE(SIZE), COLUMNNAME_3 DATATYPE(SIZE), PRIMARY KEY(COLUMNNAME) );
Here is the CREATE TABLE command we use to create the table.
TABLE-NAME – Name of the table.
COLUMNNAME_1 – Name of the columns.
DATATYPE -Type of data to be stored (integer, varchar, etc.).
SIZE – Size of data we can store in a column. The column can store a maximum of 10 digits in a number. For example, the column name specifies a data type integer with a size of 10.
The example of the CREATE TABLE is as follows:
CREATE TABLE TEAMS (NUMBER_OF_MATCHES INT(10) NOT NULL, TEAM_NAME VARCHAR(50) NOT NULL, HEAD_COACH VARCHAR(50) NOT NULL, CAPTAIN_NAME VARCHAR(50) NOT NULL, NUMBER_MATCHES_WON INT(10) NOT NULL, NUMBER_MATCHES_LOSSINT(10) NOT NULL, PRIMARY KEY(NUMBER_OF_MATCHES) );
In the above example, we created a table with the table name "TEAMS",
NUMBER_OF_MATCHES, NUMBER_MATCHES_WON, NUMBER_MATCHES_LOSS columns are of integer data type and will hold an integer with size 10.
TEAM_NAME, HEAD_COACH, and CAPTAIN_NAME columns are type varchar and will hold characters with a maximum length of 50.
NUMBER_OF_MATCHES is a Primary key in a table.
After creating the table successfully, if you want to check again or verify, you can use DESC commands as follows:
DESC TEAMS;
The output of the above query is as follows:
Field | Type | Null | Key | Default | Extra |
NUMBER_OF_MATCHES | int(10) | NO | PRI | NULL | |
TEAM_NAME | varchar(50) | NO | NULL | ||
HEAD_COACH | varchar(50) | NO | NULL | ||
CAPTAIN_NAME | varchar(50) | NO | NULL | ||
NUMBER_MATCHES_WON | int(10) | NO | NULL | ||
NUMBER_MATCHES_LOSS | int(10) | NO | NULL |

Now you have the TEAMS table in the database.
Now you can store data in table TEAMS.
Let's take another example by creating a new table for better understanding.
The example of the CREATE TABLE is as follows:
CREATE TABLE PLAYER( NUMBER_OF_MATCHES INT(10) NOT NULL, NUMBER_OF_INNINGSINT(10) NOT NULL, PLAYER_NAME VARCHAR(50) NOT NULL, TEAM_NAMEVARCHAR(50) NOT NULL, RUNS INT(10) NOT NULL, HALF_CENTURY INT(10) NOT NULL, CENTURY INT(10) NOT NULL, HIGESHT_SCORE INT(10) NOT NULL, PRIMARY KEY(NUMBER_OF_MATCHES) );
We have created another table name, "PLAYER," with the same command CREATE TABLE.
NUMBER_OF_MATCHES, RUNS, HALF_CENTURY, CENTURY, HIGESHT_SCORE columns are of integer data type and will hold an integer with size 10.
PLAYER_NAME, TEAM_NAME columns are of type varchar and will hold characters with a maximum length of 50.
NUMBER_OF_MATCHES is a Primary key in a table.
After creating the table successfully, if you want to check again or verify, you can use DESC commands as follows:
DESC TEAMS;
The output of the above query is as follows:
Field | Type | Null | Key | Default | Extra |
NUMBER_OF_MATCHES | int(10) | NO | PRI | NULL | |
NUMBER_OF_INNINGS | int(10) | NO | NULL | ||
PLAYER_NAME | varchar(50) | NO | NULL | ||
TEAM_NAME | varchar(50) | NO | NULL | ||
RUNS | int(10) | NO | NULL | ||
HALF_CENTURY | int(10) | NO | NULL | ||
CENTURY | int(10) | NO | NULL | ||
HIGHEST_SCORE | int(10) | NO | NULL |

CREATE TABLE USING AN EXISTING (ANOTHER) TABLE:
If you want a copy of an existing table, we can create using the CREATE TABLE command.
The new table will get the same column definitions.
The new table will have all columns of an existing table or can select the specified columns.
If we created a new table from an existing table, the new table would have all the old values of an existing table.
The syntax of the create table using an existing table is as follows:
CREATE TABLE TABLE_NAME AS SELECT * FROM EXISITING_TABLE_NAME;
Above syntax creates a new table from an existing table, selecting all the columns defined in an existing table.
TABLE_NAME – the new name of the table
EXISITING_TABLE_NAME - old table
The example of the CREATE TABLE USING AN EXISTING TABLE is as follows:
CREATE TABLE ODIS AS SELECT * FROM ODI;
We already have an ODI table in our system. To create a copy table of an “ODI," we run the above query to create a copy of an existing table “ODI”
ODIS - NEW_TABLE_NAME.
ODI - OLD TABLE NAME
After creating the table successfully, if you want to check again or verify, you can use DESC commands as follows:
DESC ODIS;
The output of the above query is as follows:
Field | Type | Null | Key | Default | Extra |
Matches | int(11) | YES | NULL | ||
P_Name | char(200) | YES | NULL |

Let’s create another new table from an existing table.
The example of the CREATE TABLE USING AN EXISTING TABLE is as follows:
CREATE TABLE TEAM AS SELECT * FROM TEAMS;
The above query creates a new table, "TEAM," from an existing table, "TEAMS," which defines all the existing table's columns into the new table.
After creating the table successfully, if you want to check again or verify, you can use DESC commands as follows:
DESC TEAM;
The output of the above query is as follows:
Field | Type | Null | Key | Default | Extra |
NUMBER_OF_MATCHES | int(10) | NO | NULL | ||
TEAM_NAME | varchar(50) | NO | NULL | ||
HEAD_COACH | varchar(50) | NO | NULL | ||
CAPTAIN_NAME | varchar(50) | NO | NULL | ||
NUMBER_MATCHES_WON | int(10) | NO | NULL | ||
NUMBER_MATCHES_LOSS | int(10) | NO | NULL |

If you want only a selected column into a new table from an existing table, then we will use the below query as follows: -
The syntax is as follows:
CREATE TABLE NEW_TABLE AS SELECT COLUMNNAME_1, COLUMNNAME_2, FROM EXISITING_TABLE_NAME;
The example is as follows:
CREATE TABLE PLAYERS AS SELECT NUMBER_OF_MATCHES, NUMBER_OF_INNINGS, PLAYER_NAME, TEAM_NAME FROM PLAYER;
The above query creates a new table "PLAYERS” from an existing table “PLAYER," which has only selected columns from an existing table into a new table (NUMBER_OF_MATCHES, NUMBER_OF_INNINGS, PLAYER_NAME, TEAM_NAME)
After creating the table successfully, if you want to check again or verify, you can use DESC commands as follows:
DESC PLAYERS;
The output of the above query is as follows:
Field | Type | Null | Key | Default | Extra |
NUMBER_OF_MATCHES | int(10) | NO | NULL | ||
NUMBER_OF_INNINGS | int(10) | NO | NULL | ||
PLAYER_NAME | varchar(50) | NO | NULL | ||
TEAM_NAME | varchar(50) | NO | NULL |
