SQL Tutorial SQL Advanced SQL Database SQL References

SQL CREATE TABLE Keyword



The SQL CREATE TABLE keyword is used to create a new table. Creating a table involves providing a name to the table and defining name and data type (e.g. varchar, integer, date, etc.) of each column.

Syntax

The syntax of using CREATE TABLE keyword is given below:

CREATE TABLE table_name (
  column1 datatype,
  column2 datatype,
  column3 datatype,
  .....
  .....
);

Example: Create a table

The mentioned below statement creates a table called Employee which contains five columns: EmpID, Name, City, Age and Salary.

CREATE TABLE Employee (
  EmpID INT NOT NULL PRIMARY KEY,
  Name VARCHAR(255) NOT NULL,
  City VARCHAR(100),
  Age INT,
  Salary DECIMAL(18,2)
);

In the above statement, the data type specifies what type of data the column can hold. After the data type, optional attributes can be provided for each column, like NOT NULL is used to ensure that a column cannot have a NULL value. See SQL Constraints for more information.

The above statement will create a empty table named Employee containing five columns. When the table is created successfully it displays a message. Along with this in databases like MySQL and MariaDB, it can also be checked using DESC command as follows:

DESC Employee;

This will produce the result as shown below:

FieldTypeNullKeyDefaultExtra
EmpIDint(11)NoPRI
Namevarchar(255)No
Cityvarchar(100)YesNULL
Ageint(11)YesNULL
Salarydecimal(18,2)YesNULL

Similarly in SQL Server, the description of the table can be checked using EXEC sp_columns command as shown below:

EXEC sp_columns Employee; 

This result of the above code will be similar to:

TABLE_OWNERTABLE_NAMECOLUMN_NAMEDATA_TYPETYPE_NAMEPRECISIONLENGTH
dboEmployeeEmpID4int identity104
dboEmployeeName12varchar255255
dboEmployeeCity12varchar100100
dboEmployeeAge4int104
dboEmployeeSalary3decimal1820

This indicates that the Employee is now available in the database which can be used to store information related to employee.

Create a table using Another table

The following keywords are used to create a table from an existing table:

CREATE TABLE AS Keyword

In databases like MySQL and MariaDB, the CREATE TABLE AS keyword is used to create a table from an existing table by copying the existing table's columns. When creating a table in this way, the new table will be populated with the records from the existing table (based on the SELECT statement).

Syntax

The syntax for using CREATE TABLE AS keyword given below:

/* copy all columns from a table */
CREATE TABLE new_table
  AS (SELECT * FROM old_table);

/* copy selected columns from a table */
CREATE TABLE new_table
  AS (SELECT column_1, column2, ...
      FROM old_table);

/* copy columns from multiple table */
CREATE TABLE new_table
  AS (SELECT column_1, column2, ...
      FROM old_table_1, old_table_2, ...);

Example:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • Copy all columns of a table: The below statement is used to create a new table called Employee_PT by copying all columns of Employee table. The new table will contain those records of Employee table where the age of the employee is less than 28.

    CREATE TABLE Employee_PT
      AS (SELECT * FROM Employee
        WHERE Age < 28);
    
    -- see the result
    SELECT * FROM Employee_PT;
    

    This will produce the result as shown below:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    2MarryNew York242750
    3JoParis272800
  • Copy selected columns of a table: It is also possible to copy selected columns when using CREATE TABLE AS statement. In the example below, the columns which are copied are: EmpID, Name and City.

    CREATE TABLE Employee_PT
      AS (SELECT EmpID, Name, City FROM Employee
        WHERE Age < 28);
    
    -- see the result
    SELECT * FROM Employee_PT;
    

    This will produce the result as shown below:

    EmpIDNameCity
    1JohnLondon
    2MarryNew York
    3JoParis
  • Using AS clause: The AS clause can be used to rename the column name. See the example below:

    CREATE TABLE Employee_PT
      AS (SELECT EmpID, Name AS EmployeeName, City FROM Employee
        WHERE Age < 28);
    
    -- see the result
    SELECT * FROM Employee_PT;
    

    This will produce the result as shown below:

    EmpIDEmployeeNameCity
    1JohnLondon
    2MarryNew York
    3JoParis
  • Copy columns from multiple tables: Consider one more table called Contact_Info with the following records:

    Phone_NumberEmpIDAddressGender
    +1-80540980002Brooklyn, New York, USAF
    +33-1479961013Grenelle, Paris, FranceM
    +31-2011503194Geuzenveld, Amsterdam, NetherlandsF
    +86-10997324586Yizhuangzhen, Beijing, ChinaM
    +65-672348247Yishun, SingaporeM
    +81-3577990728Koto City, Tokyo, JapanM

    The below statement is used to create a new table called Employee_PT which will contain selected columns of Employee and Contact_Info tables. The new table will contain records based on INNER JOIN.

    CREATE TABLE Employee_PT
      AS (SELECT A.EmpID, A.Name, A.City, B.Address 
        FROM Employee A
        INNER JOIN Contact_Info B
        ON A.EmpID = B.EmpID);
    
    -- see the result
    SELECT * FROM Employee_PT;
    

    This will produce the result as shown below:

    EmpIDNameCityAddress
    2MarryNew YorkBrooklyn, New York, USA
    3JoParisGrenelle, Paris, France
    4KimAmsterdamGeuzenveld, Amsterdam, Netherlands
    6HuangBeijingYizhuangzhen, Beijing, China

SELECT INTO Keyword

In databases like SQL Server, the SELECT INTO keyword is used to create a table from an existing table by copying the existing table's columns. When creating a table in this way, the new table will be populated with the records from the existing table (based on the SELECT statement).

Syntax

The syntax for using SELECT INTO keyword is given below:

/* copy all columns into new_table */
SELECT *
INTO new_table
FROM old_table
WHERE condition;

/* copy selected columns into new_table */
SELECT column1, column2, column3, ...
INTO new_table
FROM old_table
WHERE condition;

Example:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • Copy all columns of a table: The below statement is used to copy all columns of Employee table into a new table called Employee_PT. The new table will contain all records of Employee table where the age of the employee is less than 28.

    SELECT * INTO Employee_PT 
    FROM Employee
    WHERE Age < 28;
    
    -- see the result
    SELECT * FROM Employee_PT;
    

    This will produce the result as shown below:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    2MarryNew York242750
    3JoParis272800
  • Copy selected columns of a table: It is also possible to copy selected columns when using SELECT INTO statement. In the example below, the columns which are copied are: EmpID, Name and City.

    SELECT EmpID, Name, City INTO Employee_PT 
    FROM Employee
    WHERE Age < 28;
    
    -- see the result
    SELECT * FROM Employee_PT;
    

    This will produce the result as shown below:

    EmpIDNameCity
    1JohnLondon
    2MarryNew York
    3JoParis
  • Using AS clause: The AS clause can be used to rename the column name. See the example below:

    SELECT EmpID, Name AS EmployeeName, City INTO Employee_PT 
    FROM Employee
    WHERE Age < 28;
    
    -- see the result
    SELECT * FROM Employee_PT;
    

    This will produce the result as shown below:

    EmpIDEmployeeNameCity
    1JohnLondon
    2MarryNew York
    3JoParis

❮ SQL Keywords