Resetting the primary key to 1 after deleting all the data

我的未来我决定 提交于 2019-11-27 18:25:26

问题


So i have MySql and i have a table user with a user_id column and it is the primary key and auto incremented. Now when i delete all my data from the table and add the new one, the user_id does not start from 1 but from the number it had before deletion. What if i want to reset it without dropping the whole table and creating it again.


回答1:


ALTER TABLE some_table AUTO_INCREMENT=1

So some_table would be the table you want to alter.

You could also use:

TRUNCATE TABLE some_table

This will reset the Auto Increment on the table as well as deleting all records from that table.




回答2:


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

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



回答3:


you can use DBCC check identity to reset your Primary key. here is the Sytax:

DBCC CHECKIDENT(TableName,RESEED,0)



来源:https://stackoverflow.com/questions/6972275/resetting-the-primary-key-to-1-after-deleting-all-the-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!