MySQL
LAST_INSERT_ID
SQL
Database
Auto-Increment

LAST_INSERT_ID MySQL

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

MySQL is renowned for its robust set of functions, offering flexibility and ease for developers working with relational databases. One such function, `LAST_INSERT_ID()`, is widely used in scenarios where it's necessary to obtain the unique ID of the last inserted row in tables with an auto-increment column. This article explores the technicalities of `LAST_INSERT_ID()`, practical applications, and caveats to consider.

Understanding `LAST_INSERT_ID()`

The `LAST_INSERT_ID()` function serves to return the last automatically generated `AUTO_INCREMENT` value generated by the previous `INSERT` statement. It is particularly useful in databases with one or more tables designed to automatically generate unique keys for entries.

Technical Explanation

Here's a breakdown of how `LAST_INSERT_ID()` works:

  1. Automatic Retrieval: The function retrieves the most recent `AUTO_INCREMENT` value from the current database connection. This ensures that, even in a multi-user environment, the caller gets the correct ID, specific to their connection.
  2. Transaction Safe: Within transactions, if a rollback occurs, the `AUTO_INCREMENT` value does not roll back, maintaining consistency for future transactions.
  3. Non-blocking: Because `LAST_INSERT_ID()` is session-specific, operations from other connections do not affect the result of `LAST_INSERT_ID()`, ensuring separate operations remain isolated.

Practical Example

Consider a scenario where you have a table `Employees`, which allocates unique employee IDs through an auto-incrementing primary key:

  • Order Processing Systems: When creating an order, retrieving the order ID for use in associated tables.
  • User Registration: Obtaining a user's unique ID immediately after registration for further processing, such as assigning default settings.
  • Multiple Inserts: If an `INSERT` statement inserts multiple rows, such as through `INSERT ... SELECT`, `LAST_INSERT_ID()` returns the first inserted ID.
  • Updates: Calling `LAST_INSERT_ID()` after an `UPDATE` operation will not yield meaningful results, as no new IDs are generally generated.
  • Stored Procedures: When using stored procedures, `LAST_INSERT_ID()` returns the most recent ID generated within the procedure's scope.
  • Manual `AUTO_INCREMENT`: If you manually set an `AUTO_INCREMENT` value in an `INSERT`, this value is what `LAST_INSERT_ID()` returns.
  • Threaded Applications: Ensure the database connection is properly managed in multi-threaded applications to prevent confusion with values returned.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.