Batch insert SQL statement

人盡茶涼 提交于 2019-12-23 02:21:09

问题


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.


回答1:


INSERT INTO tbl (col1, col2) VALUES ('val1', 'val2'), ('val3', 'val4'), ...

And please read documentation first next time




回答2:


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”.



来源:https://stackoverflow.com/questions/10429348/batch-insert-sql-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!