Inserting Data into MySQL Table

INSERT statement allows you to insert one or more rows to the table. In MySQL, the INSERT statement form is listed as follows:

INSERT INTO Syntax

INSERT INTO table_name VALUES (value1, value2, value3,…)

INSERT INTO with column names specified

The second form specifies both the column names and the values to be inserted:

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

INSERT INTO using SET

The third form uses SET coln_name = value format.

INSERT INTO table_name SET column1=value,…

INSERT…SELECT Syntax

Another form of INSERT is with INSERT … SELECT Syntax. You can quickly insert many rows into a table from one or many tables.

INSERT INTO tbl_name [(col_name…)] SELECT …

Example:
We create a temporary table ‘tmp_dept’ and insert the data from the ‘departments’ table as follows.

INSERT INTO tmp_dept(d_id, d_name) SELECT dept_no, dept_name FROM departments;

Leave a Comment