Mysql – Batch insert SQL statement

batch-filebulkinsertMySQL

I have say 100 rows data to insert in MySQL database table.
But i dont want to write all 100 INSERT statements.
Is there any bulk insert Statement in SQL ???
Please help with code if possible.

Best Answer

As the MySQL manual states:

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas. Example:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

Furthermore, from the section on speed of INSERT:

If you are inserting many rows from the same client at the same time, use INSERT statements with multiple VALUES lists to insert several rows at a time. This is considerably faster (many times faster in some cases) than using separate single-row INSERT statements. If you are adding data to a nonempty table, you can tune the bulk_insert_buffer_size variable to make data insertion even faster. See Section 5.1.3, “Server System Variables”.