×

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

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.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 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 INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 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.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.