Bug #8905 INSERT IGNORE / LAST_INSERT_ID() interaction improperly documented
Description:
The manual states the following:
>> http://dev.mysql.com/doc/mysql/en/information-functions.html <<
"If you use INSERT IGNORE and the record is ignored, the AUTO_INCREMENT counter still is incremented and LAST_INSERT_ID() returns the new value."
This is true for 4.0, but is no longer true for 4.1, which returns 0 in this case.
Personally, I think it should return NULL (in all versions) if an INSERT IGNORE fails to insert a row, but that would likely break replication if it were changed at this point. The change between versions should at least be documented.
How to repeat:
On 4.0:
>>>>>
mysql> create table test_insert (id int not null auto_increment, x int, primary key(id), unique(x));
Query OK, 0 rows affected (0.01 sec)
mysql> insert ignore into test_insert (x) values (5);
Query OK, 1 row affected (0.00 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> insert ignore into test_insert (x) values (5);
Query OK, 0 rows affected (0.00 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)
mysql> insert ignore into test_insert (x) values (5);
Query OK, 0 rows affected (0.01 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)
<<<<<
On 4.1:
>>>>>
mysql> create table test_insert (id int not null auto_increment, x int, primary key(id), unique(x));
Query OK, 0 rows affected (0.01 sec)
mysql> insert ignore into test_insert (x) values (5);
Query OK, 1 row affected (0.30 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.02 sec)
mysql> insert ignore into test_insert (x) values (5);
Query OK, 0 rows affected (0.00 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
<<<<<
Suggested fix:
Add proper documentation.
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.050 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.