SQL INSERT statement is a SQL query. It is used to insert a new record in a table.

By using SQL INSERT statement we can insert single or multiple records at a time.

There are two ways to insert data in a table:

  1. By SQL insert into statement
    1. By specifying column names
    2. Without specifying column names
  2. By SQL insert into select statement

By SQL insert into statement

1.   By specifying column names

If you specify the column names, syntax of the insert into statement will be as follows:

    INSERT INTO TABLE_NAME(col1, col2, col3,.... col N)   
           VALUES (value1, value2, value 3, .... Value N);  

Following insert into statement specify column names for insert a record in product table.

INSERT INTO PRODUCT(ProductName,Model,Brand,Price)
        VALUES('LAPTOP','E732Z','emachines',25000)

2.   Without specifying column names 

But, if you do not want specify column names, syntax of the insert into statement will be as follows:

      INSERT INTO TABLE_NAME  
      VALUES (value1, value2, value 3, .... Value N);

 

Following insert into statement Without specify column names insert a record in product table.

    INSERT INTO PRODUCT 
        VALUES('HP LAPTOP ','E7452Z','emachines',50000)

 By SQL insert into select statement

If you want use select statement for inserting record with insert into statement will be as follows:

INSERT INTO table_name (column1, column2, .... column)  
SELECT column1, column2, .... ColumnFROM table_name [WHERE condition];

Following insert into statement used to select statement to inserting records in product table from PRODUCT_MASTE table.

 

INSERT INTO PRODUCT(ProductName,Model,Brand,Price) 
            SELECT ProductName,Model,Brand,Price FROM PRODUCT_MASTER

Note: when you want to insert records by using select statement, you should make sure that data type of the value and the column should be matched.


Thanks.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.