×

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:

FieldTypeNullKeyDefaultExtra
NUMBER_OF_MATCHESint(10)NOPRINULL 
TEAM_NAMEvarchar(50)NO NULL 
HEAD_COACHvarchar(50)NO NULL 
CAPTAIN_NAMEvarchar(50)NO NULL 
NUMBER_MATCHES_WONint(10)NO NULL 
NUMBER_MATCHES_LOSSint(10)NO NULL 
SQL CREATE TABLE

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:

FieldTypeNullKeyDefaultExtra
NUMBER_OF_MATCHESint(10)NOPRINULL 
NUMBER_OF_INNINGSint(10)NO NULL 
PLAYER_NAMEvarchar(50)NO NULL 
TEAM_NAMEvarchar(50)NO NULL 
RUNSint(10)NO NULL 
HALF_CENTURYint(10)NO NULL 
CENTURYint(10)NO NULL 
HIGHEST_SCOREint(10)NO NULL 
SQL CREATE TABLE

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:

FieldTypeNullKeyDefaultExtra
Matchesint(11)YES NULL 
P_Namechar(200)YES NULL 
SQL CREATE TABLE

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:

FieldTypeNullKeyDefaultExtra
NUMBER_OF_MATCHESint(10)NO NULL 
TEAM_NAMEvarchar(50)NO NULL 
HEAD_COACHvarchar(50)NO NULL 
CAPTAIN_NAMEvarchar(50)NO NULL 
NUMBER_MATCHES_WONint(10)NO NULL 
NUMBER_MATCHES_LOSSint(10)NO NULL 
SQL CREATE TABLE

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:

FieldTypeNullKeyDefaultExtra
NUMBER_OF_MATCHESint(10)NO NULL 
NUMBER_OF_INNINGSint(10)NO NULL 
PLAYER_NAMEvarchar(50)NO NULL 
TEAM_NAMEvarchar(50)NO NULL 
SQL CREATE TABLE

Related Topics

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 minutes read.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.