The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two ways:
1. Specify both the column names and the values to be inserted:
Syntax
INSERT INTO table_name ( column1, column2, column3, ...) VALUES ( value1,value2,value3,...);
Syntax
INSERT INTO table_name
VALUES ( value1,value2,value3,...);
Demo Employee table
This employee table is used for examples:
INSERT INTO Example
Example
The following SQL statement inserts a new record in the "employee" table: INSERT INTO employee ( Employee_name , Address , City , Pincode , Country )You can click on above box to edit the code and run again.
VALUES ('Krity Rana' , 'Nutannagar' ,'Hazaribagh' ,'825301' , 'India' ) ;
Output
Did you notice that we did not insert any number into the Employee_id field?
The Employee_id column is an auto-increment field and will be generated automatically when a new record is inserted into the table.
Insert Data Only in Specified Columns
It is also possible to only insert data in specific columns.
The following SQL statement will insert a new record, but only insert data in the "Employee_name", "City", and "Country" columns
(Employee_id will be updated automatically):
Example
INSERT INTO employee ( Employee_name , City , Country )You can click on above box to edit the code and run again.
VALUES ('Chandan Gupta' , 'kalkata' , 'India' ) ;
Output
Insert Multiple Rows
It is also possible to insert multiple rows in one statement.
To insert multiple rows of data, we use the same INSERT INTO statement, but with multiple values:
Example
The following SQL statement inserts a new record in the "employee" table:You can click on above box to edit the code and run again.
INSERT INTO employee ( Employee_name , Address , City , Pincode , Country )
VALUES
('Ranjeet Singh' , 'Patel Nagar,patel Rd,Block 2' ,'Delhi' ,'110008' , 'India' ), ('Ishfak Khan' , 'KD Colony,Sector 12' ,'Delhi' ,'110022' , 'India' ), ('Jagdish Khurana' , '11,Lord Sinha Road' ,'KolKata' ,'700071' , 'India' ) ;