SQL
AUTO_INCREMENT
database management
MySQL
SQL query

How to get the current AUTO_INCREMENT value of a MySQL table

Codemia Knowledge Hub ·

Run SHOW TABLE STATUS LIKE 'table_name'; and read the Auto_increment column, or query information_schema.tables for the AUTO_INCREMENT value directly. Either way, the value is the number that will be assigned to the next new row, which is useful for previewing the next ID or debugging.

AUTO_INCREMENT is usually set on a primary key column so MySQL assigns a sequential integer to each inserted row, giving every row a unique identifier even when the input data has none.

Using SHOW TABLE STATUS

SHOW TABLE STATUS returns metadata about a table, including its AUTO_INCREMENT value:

sql
SHOW TABLE STATUS LIKE 'table_name';

For a table named users:

sql
SHOW TABLE STATUS LIKE 'users';

The result might look like this:

NameEngineVersionRow_formatRowsAvg_row_lengthData_lengthMax_data_lengthIndex_lengthData_freeAuto_increment...
usersInnoDB10Dynamic5163.84819200011...

Here the next new row would get the ID 11.

Using information_schema

To get only the value, without the other status columns, query information_schema.tables:

sql
1SELECT `AUTO_INCREMENT` 
2FROM  `information_schema`.`tables`
3WHERE `table_name` = 'users'
4AND `table_schema` = 'your_database_name';

Setting and resetting AUTO_INCREMENT

To start IDs at a specific number, use ALTER TABLE:

sql
ALTER TABLE users AUTO_INCREMENT = 100;

This makes the next ID 100.

  • Increment step: MySQL does not natively support changing the increment step other than setting it globally for the server instance. You can emulate it by managing IDs manually.
  • Resetting: this typically involves dropping and recreating the table, but you can also run:
sql
  ALTER TABLE users AUTO_INCREMENT = 1;

This sets the next inserted value to 1, assuming no existing values are greater.

Caveats when relying on the value

  1. Concurrency: under high concurrency, the value you read is only a hint. Another transaction may use it before your operation completes.
  2. Data integrity: be careful with business logic that depends on sequential IDs, especially with replicated databases or sharded environments.
  3. Performance: frequent restarts or adjustments that push IDs very high can degrade performance once IDs exceed the optimized range of the column's storage type.
TopicDetails
Basic CommandSHOW TABLE STATUS LIKE 'table_name';
Alternative CommandSELECT AUTO_INCREMENT FROM information_schema.tables;
Set Initial ValueALTER TABLE table_name AUTO_INCREMENT = value;
Use CaseOptimizing space, Handling unique identifiers
ConsiderationsConcurrency, Performance, Data Integrity

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

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

Practice system design