How to reset the initial values of auto-increment columns in a MySQL table
How to reset the initial values of auto-increment columns in a MySQL table
1. Question proposal
In MySQL database design, auto-increment numeric columns are generally designed to serve as the primary key unrelated to the business. After a database is frequently deleted or cleared, its auto-increment value will automatically increase. What should I do if I need to start again?
2. Solution
A. alter table
Delete from table_name; alter table table_name AUTO_INCREMENT = 1;
If there is a lot of data in the database table, the delete operation will last for a long time. Pay attention to this problem.
B. truncate
Truncate table_name
It is simple and quick to clear data directly.
3. delete vs tuncate
The main differences are as follows:
- Truncate is fast and does not contain logs. Therefore, rollback cannot be performed. Delete.
- Truncate will reset the index and auto-increment initial values, and delete will not
- Truncate does not trigger, while delete triggers.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!