MySQL Insert Into Statement

MySQL Tutorials

MySQL INSERT INTO statement is used to insert records in the table. This statement comes under Data Manipulation Language. There are many ways to use INSERT INTO statement in MySQL. So let’s understand these ways

Let’s assume we have a table structure of Employee Table

MySQL : Employee Table Structure
MySQL: Employee Table Structure

 

Now let’s understand INSERT INTO syntax and how can we use in MySQL database?

When we need to insert the record in all the column and in the sequence then we can use the syntax:

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

So if we want to insert a row in the employee table as we have described above. So our SQL Query will be like:

INSERT INTO employee
VALUES (7654, 'ALEX', 'SALESMAN',7698,"09/28/1981",1250,200,30);

Note: if you want to use above syntax then values for all the column you want to insert have to be in sequence

Let’s understand another syntax for “INSERT INTO” in MySQL:

This syntax can be used when we don’t want to insert all column at once or we don’t want to insert records in sequence.

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Using above syntax we can insert a row in the Employee Table as mentioned below:

INSERT INTO employee (emp_no, ename, job)
VALUES (7654, 'ALEX', 'SALESMAN');

or  the same syntax can also be written using ‘SET’ Keyword in MySQL

INSERT INTO employee SET
emp_no = 7654, ename='ALEX', job = 'SALESMAN';

One more thing left from our side regarding the INSERT INTO statement in MySQL.

If you want to insert multiple rows all in once.

Mulitple Insertion of Rows in MySQL

This syntax will be used when you want to insert multiple rows and in all columns and in sequence

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

This syntax will be used when you want to insert multiple rows and in limited columns.

INSERT INTO table_name (col1,col2,col3)
VALUES (value1, value2, value3),(value1, value2, value3),(value1, value2, value3),(value1, value2, value3);

For more insert statement read our article on interesting facts in Insert Into Statement