I want to set auto increment to 1 by below command, but it doesn't works:
ALTER TABLE tablename AUTO_INCREMENT = 1
I searched and found out that I should use TRUNCATE TABLE
. But this command fails due to foreign key constraint. Any idea?
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.
Sign up to join this communityI want to set auto increment to 1 by below command, but it doesn't works:
ALTER TABLE tablename AUTO_INCREMENT = 1
I searched and found out that I should use TRUNCATE TABLE
. But this command fails due to foreign key constraint. Any idea?
If you ever need ALTER TABLE tablename AUTO_INCREMENT = 1
you are probably misusing AUTO_INCREMENT
. AUTO_INCREMENT
is designed to provide unique keys, nothing more. If you need it to be "1", you should invent your own mechanism; do not trust AUTO_INCREMENT
.
Would you care to explain your purpose? Perhaps we can help you find a suitable workaround. (No "setting it to 1" is not the purpose, it is your attempted solution to some higher purpose. Step back.)
INSERT IGNORE
, IODKU, REPLACE
, etc. This discusses a way to avoid such during batch normalization.
Jul 15, 2016 at 16:53