©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The LAST_INSERT_ID SQL Function in MySQL and MariaDB - Last AUTO_INCREMENT value

LAST_INSERT_IDSyntax:LAST_INSERT_ID([Expression])
Return value:INTEGER
Function type:Information function
Function Description

The LAST_INSERT_ID() function returns the first automatically generated value in an INSERT statement, witch in an AUTO_INCREMENT column was inserted.

If no rows have been inserted, the value of LAST_INSERT_ID() remains unchanged.

If LAST_INSERT_ID() is called with the argument "Expression", the function returns the value of "Expression" and the next call of LAST_INSERT_ID() returns the same value if no INSERT statement that generated an AUTO_INCREMENT value was previously executed.

If no rows were inserted successfully, the LAST_INSERT_ID() function returns 0 or the last AUTO_INCREMENT value of a previous INSERT statement.

For failed transactions, the value of LAST_INSERT_ID() remains undefined.

Executing ROLLBACK does not reset the value of LAST_INSERT_ID() to the value before the transaction. It remains as it was at the time before ROLLBACK.


For stored routines (procedures, functions, or triggers), the value of LAST_INSERT_ID() changes as follows:

If a stored procedure is executed that changes the value of LAST_INSERT_ID(), the new value will also be seen by statements following the procedure call.

Stored functions and triggers restoring the value they had before they were called, so following statements will not see a changed value.

SQL Examples for the LAST_INSERT_ID function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

DROP TABLE IF EXISTS `test_table`;
    
CREATE TABLE `test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `type` enum('First','Second','Third','Last'DEFAULT NULL,
  `num` int(11) NOT NULL DEFAULT 0,
  `price` decimal(10,2) NOT NULL DEFAULT 0.00,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
ENGINE=InnoDB;

INSERT INTO `test_table` (`id`,`name`,`type`,`num`,`price`VALUES 
(1,'Art 1','First',1,10.50),
(2,'Art 2','Second',3,12.50),
(3,'Art 3','Second',2,15.50);

select last_insert_id(); /* returns 0, No Value */

truncate `test_table`;

INSERT INTO `test_table` (`name`,`type`,`num`,`price`VALUES 
('Art 1','First',1,10.50),
('Art 2','Second',3,12.50),
('Art 3','Second',2,15.50);

select last_insert_id(); /* returns 1, First Value */

select * from `test_table`;

INSERT INTO `test_table` (`name`,`type`,`num`,`price`VALUES 
('Art 4','Last',1,10.50);

select last_insert_id(); /* returns 4, Last Value */

select * from `test_table`;

select last_insert_id(12); /* returns 12 */
select last_insert_id(); /* returns 12 */

INSERT INTO `test_table` (`name`,`type`,`num`,`price`VALUES 
('Art 5','Last',1,10.50);

select last_insert_id(); /* returns 5, Last Value */

last_insert_id()
bigint(21)
0
last_insert_id()
bigint(21)
1
id
name
type
num
price
int(11)varchar(20) LATIN1enum LATIN1int(11)decimal(10,2)
1Art 1First110.50
2Art 2Second312.50
3Art 3Second215.50
last_insert_id()
bigint(21)
4
id
name
type
num
price
int(11)varchar(20) LATIN1enum LATIN1int(11)decimal(10,2)
1Art 1First110.50
2Art 2Second312.50
3Art 3Second215.50
4Art 4Last110.50
last_insert_id(12)
bigint(2)
12
last_insert_id()
bigint(21)
12
last_insert_id()
bigint(21)
5
The examples were created with the MyWAY SQL manager: Download

How to use the LAST_INSERT_ID() function in MySQL and MariaDB databases

In MySQL and MariaDB, the LAST_INSERT_ID() function is used to retrieve the value used for a AUTO_INCREMENT column was generated during the last INSERT operation. The function requires no arguments, returns the last auto-generated value within the current session, and can be used within SQL statements or transactions.

This function can be used, for example, to retrieve the last generated primary key value for further processing or for referencing in other tables. For multi-line INSERT operations that generate multiple AUTO_INCREMENT values, the LAST_INSERT_ID() function returns only the last generated value. The returned value can also be used to join with other related data in application logic or to maintain data consistency across multiple tables.

The function returns the last generated value within the current session and therefore may not provide accurate results when multiple sessions are performing INSERT operations at the same time.
IS_IPV6IS_UUIDISNULLLAST_INSERT_IDLAST_VALUELASTVALLCASE

Further MySQL und MariaDB SQL Information functions

BENCHMARKBENCHMARK(Number, Expression)
More about BENCHMARK Function

BINLOG_GTID_POSBINLOG_GTID_POS()

CHARSETCHARSET(String)
More about CHARSET Function

COERCIBILITYCOERCIBILITY(String)
More about COERCIBILITY Function

COLLATIONCOLLATION(String)
More about COLLATION Function

CONNECTION_IDCONNECTION_ID()
More about CONNECTION_ID Function

CURRENT_USERCURRENT_USER()
More about CURRENT_USER Function

CURRENT_ROLECURRENT_ROLE()
More about CURRENT_ROLE Function

DATABASEDATABASE()
More about DATABASE Function

DECODE_HISTOGRAMDECODE_HISTOGRAM()

DEFAULTDEFAULT(Column)
More about DEFAULT Function

FOUND_ROWSFOUND_ROWS()
More about FOUND_ROWS Function

LAST_VALUELAST_VALUE(Expression [, Expression ...])
More about LAST_VALUE Function

ROWNUMROWNUM()

ROW_COUNTROW_COUNT()
More about ROW_COUNT Function

SCHEMASCHEMA()
More about SCHEMA Function

SESSION_USERSESSION_USER()
More about SESSION_USER Function

SYSTEM_USERSYSTEM_USER()
More about SYSTEM_USER Function

USERUSER()
More about USER Function

VERSIONVERSION()
More about VERSION Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the LAST_INSERT_ID SQL function: mysql.com and mariadb.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer