i have a table with ID , this ID is auto increment and primary key ,the first time i inserted to that table , the ID starts from 0 , but after i removing all the values from that table and inserted again to it , the ID doesn't start from 0 , but starts from the last value that ID had , what is going wrong please ? can i reset the value to 0 ?
You may either truncate the table using
TRUNCATE `table`
Truncate will delete all of the data in the table. Note that using TRUNCATE
will also not fire any DELETE triggers like DELETE
.
Or you can reset the count using
ALTER TABLE `table` AUTO_INCREMENT = 1
It is however the expected behavior. Don't do the latter unless the table is truly empty.
To insert a primary key with value 0, execute first the following request :
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
Removing all the elements in a table does not affect the primary key. The primary key will still autoincrement from its last value.
If you want to reset the primary key, you can try to truncate the table:
TRUNCATE TABLE yourtable;
This clears all the elements from yourtable. Whether it resets the primary key depends on your database. With Mysql it should work, as Mysql deletes and recreates the table on TRUNCATE.
You have to use the TRUNCATE statement to restart the auto_increment from 1. It's the normal behavior to avoid braking things.
来源:https://stackoverflow.com/questions/10753505/mysql-id-auto-increment-doesnt-starts-from-0