How to reset the primary key of a table? How to reset the primary key of a table? mysql mysql

How to reset the primary key of a table?


alter table foo AUTO_INCREMENT = 1


You can reset the auto-increment like this:

ALTER TABLE tablename AUTO_INCREMENT = 1

But if you are relying on the autoincrement values, your program is very fragile. If you need to assign consecutive numbers to your records for your program to work you should create a separate column for that, and not use a database auto-increment ID for this purpose.


The code below is best if you have some data in the database already but want to reset the ID from 1 without deleting the data. Copy and run in SQL command

ALTER TABLE members DROP ID;ALTER TABLE members AUTO_INCREMENT = 1;ALTER TABLE members ADD ID int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;