SQL CREATE TABLE

Back to home
Logicmojo - Updated Aug 28, 2021



What is CREATE TABLE in SQL?

In SQL, the CREATE TABLE statement is used to make a table. A table is made up of rows and columns, as we all know. As a result, while constructing tables, we must give SQL with all relevant information, such as the names of the columns, the type of data to be stored in the columns, the data size, and so on. Let's look at how to utilise the Construct TABLE statement to create tables in SQL in more detail.


CREATE TABLE Syntax

CREATE TABLE table_name(
column1 data_type (size),
column2 data_type (size),
column3 data_type (size),
.....
);

table_name: the table's name.
column1 is the first column's name.
data_type: The type of data that will be stored in this column.
For instance, int is used to represent integer data.
size: The maximum amount of data that can be stored in a given column.
For example, if the data type for a column is int and the size is 10, the column can store an integer value with a maximum of 10 digits.


Example to create a table :

Let's understand this by taking one example,

CREATE TABLE Student(
ROLL_NO int(3),
FirstName varchar(255),
LastName varchar(255),
Address varchar(255),
);

How to Create Table Using Another Table

CREATE TABLE can also be used to make a clone of an existing table. The column definitions are carried over to the new table. You can choose to pick all columns or just a few. When you build a new table from an existing table, the new table will be filled with the old table's values.


CREATE TABLE Syntax Using Another Table

CREATE TABLE new_table_name AS 
SELECT column1, column2,...
FROM existing_table_name
WHERE .....;

In this way a new table can be created using existing table.

Example to create a table using existing table:

Let's understand this by taking one example,


CREATE TABLE New_Customer_Table AS 
SELECT Customer_Name, Customer_Id, Moblie_No
FROM Customer

The above query will create a new table with name New_Customer_Table having Customer_Name, Customer_Id, Moblie_No fields from Customer Table.


With this article at Logicmojo, you must have the complete idea of SQL CREATE TABLE.