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.
2 Answers
As the MySQL manual states:
INSERTstatements that useVALUESsyntax 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
INSERTstatements with multipleVALUESlists to insert several rows at a time. This is considerably faster (many times faster in some cases) than using separate single-rowINSERTstatements. If you are adding data to a nonempty table, you can tune thebulk_insert_buffer_sizevariable to make data insertion even faster. See Section 5.1.3, “Server System Variables”.
Comments
INSERT INTO tbl (col1, col2) VALUES ('val1', 'val2'), ('val3', 'val4'), ...
And please read documentation first next time