Setting and Retrieving AUTO_INCREMENT Values in MySQL
2023-05-18 00:31 (3 years ago)
When setting or retrieving the auto_increment of a table in MySQL, starting from MySQL 8, the information_schema is cached. This means that when you retrieve AUTO_INCREMENT consecutively, you might get an outdated value. Here are some countermeasures for this issue.
Setting
ALTER TABLE my_table_name AUTO_INCREMENT = 1;
Retrieving
SELECT AUTO_INCREMENT
FROM information_schema.tables
WHERE TABLE_NAME = 'my_table_name';
However, there are instances where you might retrieve a cached value. You can address this using one of the following methods:
Solution A: Disable the cache of information_schema within the session
SET information_schema_stats_expiry = 0;
Solution B: Forcefully update the cache of information_schema for the table
ANALYZE TABLE my_table_name;
Reference Pages
Please rate this article (No signup or login required)
Current rating: 5.0 (2)
Related posts
Differences between ULID and UUIDv7
Fix: MySQL error 1467 'Failed to read auto-increment value from storage engine' — AutoIncrement exhausted in Django
Disable Stopword Filter in MySQL InnoDB Full-Text Index to Allow Searching for Words like "in," "by," "is"
Here's the English translation of your blog title:"A Story About Trying to JOIN Fields with Different Collations in MySQL and Getting 'Range checked for each record'"
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.
We look forward to discussing your development needs.