重置表中的 id 而不删除它
表中有 10 个条目,主键 id 设置为自动增量。如果我删除所有条目并再插入一个,它的 id 将是 11,是否可以再次将 id 重置为 1?
There are ten entries in the table and primary key id is set to autoincrement. If I delete all entries and insert one more its id will be 11, is it possible to reset ids to 1 again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DBCC CHECKIDENT("表", RESEED, 1);
您可以在 msdn、DBCC CHECKIDENT (Transact-SQL) 中了解更多信息。
DBCC CHECKIDENT("table", RESEED, 1);
You can read more at msdn, DBCC CHECKIDENT (Transact-SQL).
您可以为“1”设置任意值
You can set any value for "1"
尝试如下所示的 truncate table 命令:
它将自动增量 id 重置为 1。
如果您的表使用外键与其他表进行引用,那么您可以使用以下命令启用/禁用约束:
启用约束:
禁用约束:
Try truncate table command like below :
it will reset auto increment id to 1.
If your table is refrenced with other table using foreign key then you can enable/disable contraint using below commands :
To Enable constraint:
To Disable constraint: