Introduction

INSERT statement is used to add new rows of data to a table. There are two ways to insert record

  • Inserting the data directly to a table.
  • Inserting data to a table through a SELECT statement.

Syntax

Inserts a single row into an existing table. Specify values for all columns except the AUTO INCREMENT column.

INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,...);
  • table_name is the name of the table from which data is added.
  • Comma separated list of columns in the table surrounded by parentheses.
  • Comma separated list of values surrounded by parentheses in the VALUES clause.

Shorter form of INSERT statement is given below. List of values must have the same order as the list of columns in the table.

INSERT INTO table_name
VALUES(value1,value2,...);

INSERT statement also allows to insert multiple rows into a table using a single statement.

INSERT INTO table_name(column1,column2…)
VALUES (value1,value2,…),
       (value1,value2,…),
…

To copy data from a table to another table, use SELECT statement to select values from another table and supply them to the INSERT statement. List of columns in the SELECT clause must be corresponding to the list of columns in the INSERT INTO clause.

INSERT INTO table_name(column1, column2,...)
SELECT 
     column1, column2,...
FROM 
     another_table
WHERE 
     condition

Example

/* Insert a row to the employee table */
INSERT INTO employleinfo(id,firstName,lastName,age) VALUES(8,'Om','Singh',1)

/* Inserting data to all the columns, the column names can be omitted */
INSERT INTO employleinfo VALUES(9,'Garima','Singh',30,'Lucknow')

Reference

Inserting One or More Rows Into a Table