Posted By

kidmizere on 03/04/11


Tagged


Versions (?)

reset auto-increment in mysql


 / Published in: MySQL
 

URL: http://www.dbuggr.com/milly/reset-auto-increment-mysql/

  1. Scenario 1:
  2. If you have 100 rows, AND deleted the 100th row. The next auto increment value IS supposed to be 101. If you want the next auto increment value to be 100 instead, you can DO the following.
  3. ALTER TABLE tablename AUTO_INCREMENT = 1
  4. -- change "tablename" to the name of your table.
  5. Essentially, this ALTER statement will reset the next auto increment value to the existing largest value IN the auto increment COLUMN + 1
  6. Scenario 2:
  7. If you have 100 rows, AND deleted the 20th row. The next auto increment value IS supposed to be 101. However, if you want your next INSERT to be inserted as #20, you can use the follow “set” command immediately before your INSERT statement.
  8. SET insert_id = 20;
  9.  
  10. Scenario 3:
  11. At last, if you simply want to wipe out the data THEN reset the auto increment back to start at 1, simply run:
  12. TRUNCATE TABLE tablename;
  13. This will DELETE all the data AND reset Auto Increment back to 1

Report this snippet  

You need to login to post a comment.