When I INSERT multiple rows into a MySQL table, will the ids be increment by 1 everytime?

后端 未结 5 421
野趣味
野趣味 2020-12-17 21:06

if I have a query like the following:

INSERT INTO table (col1,col2,col3) VALUES
(\'col1_value_1\', \'col2_value_1\', \'col3_value_1\'),
(\'col1_value_2\', \'         


        
5条回答
  •  悲&欢浪女
    2020-12-17 21:41

    mysql treat multi insertion query as a transaction or one query, all rows will be inserted or if it failed there is no rows will be inserted, so if you insert this query:

    INSERT INTO table (col1,col2,col3) VALUES
    ('col1_value_1', 'col2_value_1', 'col3_value_1'),
    ('col1_value_2', 'col2_value_2', 'col3_value_2'),
    ('col1_value_3', 'col2_value_3', 'col3_value_3');
    

    mysql will run this as a one query, if your id auto incremental it will take your ids 57,58,59. if the other user pass insert query in the same time, it will 2 probability if other user query take more time than your query your query will take 57,58,59 if your query take more time than the other user so your ids will start from the other user query end. so whatever the case the multi insert query when id is auto incremental will be sorted in the query.

提交回复
热议问题