How to get the next auto-increment id in mysql
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
MySQL's auto-increment feature generates a unique sequential integer for each new row. Sometimes you need to know the next auto-increment value before inserting — for logging, pre-generating file paths, or coordinating with external systems. The main approaches are querying INFORMATION_SCHEMA.TABLES, using SHOW TABLE STATUS, and checking LAST_INSERT_ID() after an insert. Each method has limitations, especially in concurrent environments where the "next" ID can change between the time you read it and the time you insert.
Method 1: INFORMATION_SCHEMA.TABLES
This returns the next auto-increment value that will be assigned to a new row. The value comes from the table's metadata, not a lock, so it may be stale in concurrent environments.
Method 2: SHOW TABLE STATUS
The Auto_increment column in the output shows the next value. This is equivalent to the INFORMATION_SCHEMA query but returns additional table metadata.
Method 3: LAST_INSERT_ID() After Insert
LAST_INSERT_ID() is connection-specific and thread-safe. It returns the auto-increment value generated by the most recent INSERT on the current connection. This is the most reliable way to get the ID that was actually assigned.
Method 4: INSERT ... RETURNING (MySQL 8.0.21+)
MySQL does not support INSERT ... RETURNING. The standard pattern is to insert first, then call LAST_INSERT_ID().
Using LAST_INSERT_ID() in Application Code
Most database drivers provide access to the generated ID through lastrowid (Python), getGeneratedKeys() (Java), or equivalent methods. This is always preferred over querying INFORMATION_SCHEMA.
Setting and Resetting Auto-Increment
ALTER TABLE ... AUTO_INCREMENT cannot set the value lower than the current maximum ID. MySQL silently adjusts it to MAX(id) + 1.
Auto-Increment with Bulk Inserts
For multi-row inserts, LAST_INSERT_ID() returns the ID of the first row, not the last. The IDs are guaranteed to be consecutive, so you can calculate the range.
InnoDB vs MyISAM Auto-Increment Behavior
In MySQL 5.7 and earlier with InnoDB, if you insert row ID=10, delete it, and restart MySQL, the next insert gets ID=10 again. MySQL 8.0 fixed this by persisting the counter.
Common Pitfalls
- Reading the next ID before inserting is unreliable: In concurrent environments, another connection can insert between your read and your insert, giving you a different ID. Always use
LAST_INSERT_ID()after inserting instead of pre-reading the next value. - LAST_INSERT_ID() returns 0 when no auto-increment insert occurred: If you call
LAST_INSERT_ID()without a prior auto-increment insert on the current connection, it returns 0. It does not reflect other connections' inserts. - Gaps in auto-increment sequences: Rolled-back transactions, failed inserts, and InnoDB's pre-allocation all create gaps. Auto-increment IDs are not guaranteed to be sequential. Do not use them for row counting or business logic that assumes no gaps.
- ALTER TABLE AUTO_INCREMENT on large tables: This operation may lock the table briefly. On production databases with millions of rows, run it during maintenance windows.
- Mixing REPLACE INTO with auto-increment:
REPLACE INTOdeletes the existing row and inserts a new one, consuming a new auto-increment ID each time. UseINSERT ... ON DUPLICATE KEY UPDATEto preserve the original ID.
Summary
- Use
LAST_INSERT_ID()after inserting to get the assigned auto-increment value reliably - Query
INFORMATION_SCHEMA.TABLESorSHOW TABLE STATUSfor the next value (unreliable under concurrency) - Application code should use driver-specific methods (
lastrowid,getGeneratedKeys()) - Multi-row inserts return the first generated ID via
LAST_INSERT_ID() - MySQL 8.0+ persists the InnoDB auto-increment counter across restarts
- Never rely on pre-reading the next auto-increment value in concurrent applications

