MySQL Delete all rows from table and reset ID to zero

后端 未结 5 1994
野性不改
野性不改 2020-12-12 09:29

I need to delete all rows from a table but when I add a new row, I want the primary key ID, which has an auto increment, to start again from 0 respectively from 1.

5条回答
  •  伪装坚强ぢ
    2020-12-12 10:02

    If you cannot use TRUNCATE (e.g. because of foreign key constraints) you can use an alter table after deleting all rows to restart the auto_increment:

    ALTER TABLE mytable AUTO_INCREMENT = 1
    

提交回复
热议问题