This topic describes the behavior of the LAST_INSERT_ID() function in OceanBase Database and the differences between it and the corresponding function in MySQL.
The LAST_INSERT_ID() function is used to obtain the auto-increment ID value generated by the most recent INSERT operation. In OceanBase Database, the behavior of this function differs from that in MySQL. The main differences are as follows:
- Session-level LAST_INSERT_ID: This value is read and modified by using the
LAST_INSERT_ID()function. - Protocol-level LAST_INSERT_ID: This value is returned in the OK packet of the MySQL protocol.
MySQL behavior
Session-level LAST_INSERT_ID
The LAST_INSERT_ID() function is used to read and modify the LAST_INSERT_ID value for the current session:
-- Read the LAST_INSERT_ID value for the current session
SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 0 |
+------------------+
-- Modify the LAST_INSERT_ID value for the current session using the function
SELECT LAST_INSERT_ID(10);
+--------------------+
| LAST_INSERT_ID(10) |
+--------------------+
| 10 |
+--------------------+
Protocol-level LAST_INSERT_ID
The LAST_INSERT_ID value in the OK packet of the MySQL protocol. When a DML statement modifies the LAST_INSERT_ID value for the current session, the LAST_INSERT_ID value in the OK packet returned to the client after the DML statement is executed will be equivalent to the LAST_INSERT_ID value for the current session. Otherwise, the LAST_INSERT_ID value returned to the client will be the value of the AUTO_INCREMENT column for the last row inserted by the DML operator.
Compatibility with MySQL
Fully compatible behaviors
OceanBase Database is fully compatible with MySQL in the following aspects:
- Session-level
LAST_INSERT_IDreads and modifications: TheLAST_INSERT_ID()function is used to read and modify the session value. - Setting the
LAST_INSERT_IDvalue via a function expression: TheLAST_INSERT_ID(expr)function is used to set the value. - OK packet format at the protocol level: The response format to clients is consistent with MySQL.
- REPLACE statements: Returns the value of the first row's auto-increment column, whether it's an INSERT or an overwrite.
- INSERT ... ON DUPLICATE KEY UPDATE: Returns the value of the first row's auto-increment column in all cases, whether there are conflicts or not.
- Multi-row inserts: Returns the value of the first row's auto-increment column.
- IGNORE statements: If a primary key conflict prevents data from being written,
LAST_INSERT_IDis not modified.
Note
Like MySQL, OceanBase Database also stores the LAST_INSERT_ID value for a session. The behavior of OceanBase Database is consistent with MySQL when you use expressions to obtain and modify the LAST_INSERT_ID value for a session.
Behavior differences
The main differences between OceanBase Database and MySQL are as follows:
Notice
OceanBase Database currently maintains the same response format as MySQL, which includes the LAST_INSERT_ID information in the OK packet. However, this information is not fully consistent with MySQL. When you write to an AUTO_INCREMENT column using DML statements, the behavior of LAST_INSERT_ID is not fully compatible with MySQL.
Manually specifying the auto-increment column in an INSERT statement
MySQL behavior:
- If you manually specify the auto-increment column, the
LAST_INSERT_IDvalue remains unchanged.
OceanBase Database behavior:
- If you manually specify the auto-increment column, the value of the auto-increment column in the first row of the INSERT statement is returned.
Note
This is the main difference between OceanBase Database and MySQL in the behavior of LAST_INSERT_ID. OceanBase Database cannot distinguish whether the current value of the auto-increment column is manually specified or automatically generated by the auto-increment service. Therefore, the behavior of LAST_INSERT_ID is consistent in both cases.
-- Create a test table
CREATE TABLE `t1` (
`c1` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`c2` INT DEFAULT NULL,
PRIMARY KEY (`c1`),
UNIQUE KEY `c2` (`c2`)
);
-- Example 1: Modify the value using the LAST_INSERT_ID function (behavior is consistent between MySQL and OceanBase Database)
UPDATE t2 SET c1 = LAST_INSERT_ID(100) + c1 WHERE c2 = 1;
SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 100 |
+------------------+
-- Example 2: INSERT statement (behavior is consistent between MySQL and OceanBase Database)
INSERT INTO t1(c2) VALUES (1);
SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 1 |
+------------------+
-- Example 3: REPLACE statement (behavior is consistent between MySQL and OceanBase Database)
REPLACE INTO t1(c2) VALUES (2);
SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 2 |
+------------------+
-- Example 4: Multi-row insert (both MySQL and OceanBase Database return the ID of the first row)
INSERT INTO t1(c2) VALUES (3), (4);
SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 3 |
+------------------+
Detailed comparison table
| DML Statement Type | Scenario | MySQL Session's LAST_INSERT_ID | OceanBase Database Session's LAST_INSERT_ID | Description |
|---|---|---|---|---|
| INSERT | Primary key conflict | Remains unchanged | Remains unchanged | Consistent |
| No primary key conflict (auto-increment column not specified) | Value of the auto-increment column in the first row of the INSERT statement | Value of the auto-increment column in the first row of the INSERT statement | Consistent | |
| No primary key conflict (auto-increment column specified manually) | Remains unchanged | Value of the auto-increment column in the first row of the INSERT statement | OceanBase Database updates the value | |
| REPLACE | Value of the auto-increment column in the first row of the INSERT or overwrite statement | Value of the auto-increment column in the first row of the INSERT or overwrite statement | Consistent | |
| INSERT ... ON DUPLICATE KEY | No conflicts | Value of the auto-increment column in the first row of the INSERT statement | Value of the auto-increment column in the first row of the INSERT statement | Consistent |
| Partial conflicts | Value of the auto-increment column in the first row of the INSERT statement | Value of the auto-increment column in the first row of the INSERT statement | Consistent | |
| All conflicts | Remains unchanged | Remains unchanged | Consistent |
Compatibility planning
Note
The behaviors described above are available in OceanBase Database V4.3.3 and later, including V4.4.1 and later. For OceanBase Database V4.3.5, the behavior is available starting from V4.3.5 BP2.