How can I reset the AUTO_INCREMENT
of a field? I want it to start counting from 1
again.
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
- sqlyog export query result as csv
I used this in some of my scripts , the id field is droped and then added back with previous settings , all the existent fields within the database table are filled in with new auto increment values , this should also work with InnoDB .
Note that all the fields within the table will be recounted and will have other ids !!!.
it is for empty table:
if you have data but you want to tidy up it, i recommend use this :
Adding an update because the functionality changed in MySQL 5.6. As of MySQL 5.6 you CAN use the simple ALTER TABLE with InnoDB:
The docs are updated to reflect this:
http://dev.mysql.com/doc/refman/5.6/en/alter-table.html
My testing also shows that the table is NOT copied, the value is simply changed.
The auto increment counter for a table can be (re)set in two ways:
By executing a query, like others already explained:
ALTER TABLE <table_name> AUTO_INCREMENT=<table_id>;
Using Workbench or other visual database design tool. I am gonna show in Workbench how it is done - but it shouldn't be much different in other tool as well. By right click over the desired table and choosing
Alter table
from the context menu. On the bottom you can see all the available options for altering a table. ChooseOptions
and you will get this form:Then just set the desired value in the field
Auto increment
as shown in the image. This will basically execute the query shown in the first option.