LAST_INSERT_ID()

Description

The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.

When the argument is NULL, LAST_INSERT_ID() returns a value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement; if you haven’t inserted a column before, then the return value start at 1, and if you’ve inserted a column before, then the return value of the AUTO_INCREMENT column increases by 1.

The value of LAST_INSERT_ID() remains unchanged if no rows are successfully inserted.

In MySQL, if you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only. For example:

  1. mysql> CREATE TABLE t (id INT AUTO_INCREMENT NOT NULL PRIMARY KEY, name VARCHAR(10) NOT NULL);
  2. mysql> INSERT INTO t VALUES (NULL, 'Bob');
  3. mysql> SELECT * FROM t;
  4. +----+------+
  5. | id | name |
  6. +----+------+
  7. | 1 | Bob |
  8. +----+------+
  9. mysql> SELECT LAST_INSERT_ID();
  10. +------------------+
  11. | LAST_INSERT_ID() |
  12. +------------------+
  13. | 1 |
  14. +------------------+
  15. mysql> INSERT INTO t VALUES (NULL, 'Mary'), (NULL, 'Jane'), (NULL, 'Lisa');
  16. mysql> SELECT * FROM t;
  17. +----+------+
  18. | id | name |
  19. +----+------+
  20. | 1 | Bob |
  21. | 2 | Mary |
  22. | 3 | Jane |
  23. | 4 | Lisa |
  24. +----+------+
  25. mysql> SELECT LAST_INSERT_ID();
  26. +------------------+
  27. | LAST_INSERT_ID() |
  28. +------------------+
  29. | 2 |
  30. +------------------+

But in MatrixOne, we have a different behavior; if you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the last inserted row. Like the example above, when you execute INSERT INTO t VALUES (NULL, 'Mary'), (NULL, 'Jane'), (NULL, 'Lisa');, the LAST_INSERT_ID() will return 4.

Syntax

  1. LAST_INSERT_ID(), LAST_INSERT_ID(expr)

Examples

  1. create table t1(a int auto_increment primary key);
  2. insert into t1 values();
  3. mysql> select last_insert_id();
  4. +------------------+
  5. | last_insert_id() |
  6. +------------------+
  7. | 1 |
  8. +------------------+
  9. 1 row in set (0.02 sec)
  10. insert into t1 values(11);
  11. insert into t1 values();
  12. mysql> select last_insert_id();
  13. +------------------+
  14. | last_insert_id() |
  15. +------------------+
  16. | 12 |
  17. +------------------+
  18. 1 row in set (0.02 sec)
  19. insert into t1 values(null);
  20. mysql> select last_insert_id();
  21. +------------------+
  22. | last_insert_id() |
  23. +------------------+
  24. | 13 |
  25. +------------------+
  26. 1 row in set (0.02 sec)
  27. create table t2(a int auto_increment primary key);
  28. insert into t2 values();
  29. mysql> select last_insert_id();
  30. +------------------+
  31. | last_insert_id() |
  32. +------------------+
  33. | 1 |
  34. +------------------+
  35. 1 row in set (0.02 sec)
  36. insert into t2 values(100);
  37. insert into t2 values();
  38. mysql> select last_insert_id();
  39. +------------------+
  40. | last_insert_id() |
  41. +------------------+
  42. | 101 |
  43. +------------------+
  44. 1 row in set (0.02 sec)