Skip to content
Related Articles
Open in App
Not now

Related Articles

SQL CREATE TABLE

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 16 Mar, 2023
Improve Article
Save Article

In the SQL database for creating a table, we use a command called CREATE TABLE.

SQL CREATE TABLE Statement

A Table is a combination of rows and columns. For creating a table we have to define the structure of a table by adding names to columns and providing data type and size of data to be stored in columns.

Syntax:

CREATE table table_name
(
Column1 datatype (size),
column2 datatype (size),
.
.
columnN datatype(size)
);

Here table_name is name of the table, column is the name of column

SQL CREATE TABLE Example

Let us create a table to store data of Subjects, so the table name is Subject, Columns are Sub_ID, Sub_Name

CREATE TABLE Subject
(
Sub_ID INT, 
Sub_Name varchar(20)
);
Here INT and varchar are datatype, Datatype means type of data we can store, 
like for INT type, we can store integer type data in the column

Sub_ID     

Sub_Name    

  

Add data to the Table

To add data to the table, we use INSERT INTO, the syntax is as shown below:

Syntax:

//Below query adds data in specific column, (like Column1=Value1)//
Insert into Table_name(Column1, Column2, Column3)
Values (Value1, value2, value3);

//Below query adds data in table in sequence of column name(Value1 will be 
added in Column1 and so on)//
Insert into Table_name
Values (Value1, value2, value3);

//Adding multiple data in the table in one go//
Insert into Table_name
Values (Value01, value02, value03),
(Value11, value12, value13),
(Value21, value22, value23),
.
.
(ValueN1, valueN2, valueN3)

Here table_name is name of the table, column is the name of columns

Example Query: 

This query will add data in the table named Subject 

//Adding data in first row//
Insert into Subject
Values (1,'English');

//Adding data in specific column/
Insert into Subject(Sub_Name)
Values ('Hindi');

//Adding multiple data in the table in one go//
Insert into Subject
Values (1,'English'),
(2,'French'),
(2,'Science'),
(2,'Maths');

Try creating table with five columns and add data using above queries 
Sub_IDSub_name

1

English

2

French

3

Science

4

Maths

Create a Table Using Another Table

We can also use CREATE TABLE to create a copy of an existing table.

In the new table, it gets the exact column definition all columns or specific columns can be selected.

If an existing table was used to create a new table, by default the new table would be populated with the existing values ​​from the old table.

Syntax:

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

This article is contributed by Saylli Walve. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!