The INSERT INTO statement is used very frequently in MySQL and is used to insert a new row in a table.

Syntax

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

Or:

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

Following examples show real time applications:

Example #1

Inserting a new row in the 'dept' table:

INSERT INTO dept (deptno, dname, loc) VALUES (50,'DEVELOPMENT', 'DETROIT');

Note: If the column list is not included, the values must be listed according to the default order of the columns in the table. The order can be seen using the DESCRIBE command.

Example #2

Adding a new employee by inserting specific date values:

INSERT INTO emp VALUES (2296, 'AROMANO', 'SALESMAN', 7782, STR_TO_DATE('FEB 3, 97', 'MON DD, YY'), 1300, NULL, 10);

We can also use the INSERT statement to add rows to a table where the values are derived from some other existing table. In place of the VALUES clause, we use a subquery, e.g. to insert rows from emp table to emp10 table, as seen below:

INSERT INTO emp10 SELECT * FROM emp WHERE deptno = 10;