How to reset the primary key of a table?

MysqlPrimary Key

Mysql Problem Overview


In my table tbphotos I had a 100 records. I then deleted all the records and now that I want to restart data entry I see that my primary key doesn't start from 1, but it starts from 101,

Is there any way to reset the primary key?

I am using MySQL administrator account.

Mysql Solutions


Solution 1 - Mysql

alter table foo AUTO_INCREMENT = 1

Solution 2 - Mysql

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.

Solution 3 - Mysql

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;

Solution 4 - Mysql

If you use TRUNC instead of manually deleting records, your primary key will be reset.

Solution 5 - Mysql

This is the best script for reset auto increment:

ALTER TABLE foo MODIFY your column increment int (11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKavehView Question on Stackoverflow
Solution 1 - MysqlDonnieView Answer on Stackoverflow
Solution 2 - MysqlMark ByersView Answer on Stackoverflow
Solution 3 - MysqlicynetsView Answer on Stackoverflow
Solution 4 - MysqlVladimir KocjancicView Answer on Stackoverflow
Solution 5 - Mysqlramadhan ibrahimView Answer on Stackoverflow