Whenever I do a SELECT
statement with WHERE id is NULL
directly after an INSERT
, I get the last inserted row.
I am using MySQL 5.1.73.
It happens directly in the MySQL shell; here is my console:
mysql> CREATE TABLE testing (
-> id int(11) NOT NULL AUTO_INCREMENT,
-> name VARCHAR(200),
-> PRIMARY KEY (id)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO testing (name) VALUES ('test');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM testing WHERE id IS NULL;
+----+------+
| id | name |
+----+------+
| 1 | test |
+----+------+
1 row in set (0.01 sec)
Can anyone tell me what's going on here? Is this a bug or is it a setting I am missing?
As you can see clearly from the output, the LAST_INSERT_ID()function returns the generated value of the first row successfully inserted, not the last row. C) Using MySQL LAST_INSERT_ID()function in a stored procedure First, create two tables accountsand phonesfor testing:
It is possible for mysql_store_result () to return NULL following a successful call to to the server using mysql_real_query () or mysql_query (). When this happens, it means one of the following conditions occurred: There was a malloc () failure (for example, if the result set was too large).
MySQL LAST_INSERT_ID () returns a non-negative id of the last inserted record when you have column with AUTO_INCREMENT attribute and the column is added to index. We shall take an example table and investigate the process to solve the issue. Consider the following students table. Now we shall add a column with AUTO_INCREMENT attribute.
Code language:SQL (Structured Query Language)(sql) Second, insert a new row into the messagestable. INSERTINTOmessages(description) VALUES('MySQL last_insert_id'); Code language:SQL (Structured Query Language)(sql) Third, use the MySQL LAST_INSERT_IDfunction to get the inserted value of the idcolumn: SELECTLAST_INSERT_ID();
I have found the answer myself.
My version of MySQL (5.1.73, the last available on CentOS 6) has the setting sql_auto_is_null
by default on, while newer versions don't:
╔═════════════════════════════╦══════════════════╦══════════════════╗ ║ System Variable (<= 5.5.2) ║ Name ║ sql_auto_is_null ║ ║ ║ Variable Scope ║ Session ║ ║ ║ Dynamic Variable ║ Yes ║ ╠═════════════════════════════╬══════════════════╬══════════════════╣ ║ System Variable (>= 5.5.3) ║ Name ║ sql_auto_is_null ║ ║ ║ Variable Scope ║ Global, Session ║ ║ ║ Dynamic Variable ║ Yes ║ ╠═════════════════════════════╬══════════════════╬══════════════════╣ ║ Permitted Values (<= 5.5.2) ║ Type ║ boolean ║ ║ ║ Default ║ 1 ║ ╠═════════════════════════════╬══════════════════╬══════════════════╣ ║ Permitted Values (>= 5.5.3) ║ Type ║ boolean ║ ║ ║ Default ║ 0 ║ ╚═════════════════════════════╩══════════════════╩══════════════════╝If this variable is set to 1 (the default), then after a statement that successfully inserts an automatically generated
AUTO_INCREMENT
value, you can find that value by issuing a statement of the following form:SELECT * FROM tbl_name WHERE auto_col IS NULL
If the statement returns a row, the value returned is the same as if you invoked the
LAST_INSERT_ID()
function. For details, including the return value after a multiple-row insert, see Section 12.14, “Information Functions”. If noAUTO_INCREMENT
value was successfully inserted, theSELECT
statement returns no row.The behavior of retrieving an
AUTO_INCREMENT
value by using anIS NULL
comparison is used by some ODBC programs, such as Access. See Obtaining Auto-Increment Values. This behavior can be disabled by settingsql_auto_is_null
to 0.The default value of
sql_auto_is_null
is 0 as of MySQL 5.5.3, and 1 for earlier versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With