If I insert multiple records with a loop that executes a single record insert, the last insert id returned is, as expected, the last one. But if I do a multiple records insert statement:
INSERT INTO people (name,age)
VALUES ('William',25), ('Bart',15), ('Mary',12);
Let's say the three above are the first records inserted in the table. After the insert statement I expected the last insert id to return 3, but it returned 1. The first insert id for the statement in question.
So can someone please confirm if this is the normal behavior of LAST_INSERT_ID()
in the context of multiple records INSERT statements. So I can base my code on it.
INSERT ... VALUES ...
– BarracoonINSERT IGNORE
orINSERT ... ON DUPLICATE KEY UPDATE
MySQL does not know the number of rows that will be inserted, so it will increase by and reserve one autoinc id for every possible insert under InnoDB. Though I am not sure if the ids assigned and gaps are in the same order as the rows inVALUES
in this case. – Barracoonauto-increment-offset
of something besides 1. In that case, computing the IDs by incrementing a counter wouldn't work unless you hard-coded theauto-increment-offset
value in your code as well. – Petrol