Bug #8905 INSERT IGNORE / LAST_INSERT_ID() interaction improperly documented

Submitted: 2 Mar 2005 21:32 Modified: 4 Mar 2005 18:06
Reporter: Jeremy Cole (Basic Quality Contributor) (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Documentation Severity:S3 (Non-critical)
Version:4.1 OS:n/a
Assigned to: Paul DuBois CPU Architecture:Any

[2 Mar 2005 21:32] Jeremy Cole
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.
[2 Mar 2005 21:40] Jorge del Conde
Hola J !!!

Thanks for the bug report.  Nice to see you're still contributing things :)
[4 Mar 2005 18:06] Paul DuBois
Thank you for your bug report. This issue has been addressed in the
documentation. The updated documentation will appear on our website
shortly, and will be included in the next release of the relevant
product(s).

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.