1

Is it safe to assume that the auto increment id from a insert with multiple rows will be incremented by one starting to the mysql_last_insert_id() value? Could there be a concurrent insert (from an other user session) between this multiple insert?

In this example tbl_name has an autoincrement field called id:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9); $nbAffectedRows = mysql_affected_rows(); $idFirstRowInserted = mysql_last_insert_id(); $arrayId = array(); if($nbAffectedRows > 0){ for($i = 0; $i <= $nbAffectedRows; $i++){ $arrayId[] = $idFirstRowInserted + $i; } } 

Will $arrayId always contain values incremented by one correponding to the 'id' of the rows inserted ?

2
  • 1
    One thing you should check is auto_increment_increment, which specifies whether MySQL increments by 1 or another number: show variables like 'auto_increment_increment'; Commented Jan 12, 2011 at 22:33
  • Thanks for the information. I checked it and the value is 1 (the deault value). Commented Jan 12, 2011 at 23:20

1 Answer 1

1

It will be safe to assume that, if you wrap your INSERTs in a transaction. As a side effect your query will run faster too.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you mean by creating a queue?
I've just read more on auto_inc with InnoDB, it appears there's AUTO_INC lock that is ..kept to the end of current SQL statement. dev.mysql.com/doc/refman/5.1/en/… So your query should be safe by itself
Thanks for your time German Rumm. Unfortunately, I'm using MyISAM tables.
From dev.mysql.com/doc/refman/5.0/en/… "The ID that was generated is maintained in the server on a per-connection basis.(...) This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.