Sunday, February 20, 2011

Reset mysql table autoincrement?

In mysql you can easily reset auto increment value in a table using single query.

ALTER TABLE tablename AUTO_INCREMENT = 1;

it will reset the auto increment based on existing records in that table

*) if table have existing records the auto increment value will reset to one higer than the maximum record (max value + 1) .

*) if  table have no records then it will automatically set as 1.

if you want to reset the auto increment value as 1 but need to maintain the records as such mean mysql don't allow it. example if you have records from 5,6,7 etc, you want to reset the auto increment to 1 then mysql won't allow it , it will only set 8 as auto increment value.


Technical Tags:
     AUTO_INCREMENT, auto increment , reset mysql auto increment,autoincrement reset.

High Performance MySQL: Optimization, Backups, Replication, and MoreLearning MySQLMySQL Crash Course

7 comments:

  1. Thanks to share this.great :)

    ReplyDelete
  2. Groovy. Thanks, that worked for me. Perfect.

    ReplyDelete
  3. Very useful article.
    Thanks a lot!

    ReplyDelete
  4. i have an auto increment column in users table. problem is when i insert a new record after deleting a record, auto increment column shows increased value with respect of deleted row...lets say if i deleted 4th record and then inserted a new record then it shows it as 5th record..the updation must be done automatically...
    awaiting your precious reply.....

    ReplyDelete
    Replies
    1. Run the above query after delete the record and before insert query it will reset the auto increment value to 4.so new record will insert at 4th id

      Delete
  5. Before insert a new record in to the table just run autoincrement reset above query so it will reset the autoincrement to max value+1 thats all so new record insert from 4

    ReplyDelete