重置表中的 id 而不删除它

发布于 2024-12-05 05:35:00 字数 77 浏览 5 评论 0原文

表中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栩栩如生 2024-12-12 05:35:01

DBCC CHECKIDENT("表", RESEED, 1);

您可以在 msdn、DBCC CHECKIDENT (Transact-SQL) 中了解更多信息。

DBCC CHECKIDENT("table", RESEED, 1);

You can read more at msdn, DBCC CHECKIDENT (Transact-SQL).

瞳孔里扚悲伤 2024-12-12 05:35:01
ALTER TABLE table AUTO_INCREMENT = 1;

您可以为“1”设置任意值

ALTER TABLE table AUTO_INCREMENT = 1;

You can set any value for "1"

顾铮苏瑾 2024-12-12 05:35:00

尝试如下所示的 truncate table 命令:

TRUNCATE TABLE tablename

它将自动增量 id 重置为 1。

如果您的表使用外键与其他表进行引用,那么您可以使用以下命令启用/禁用约束:

启用约束:

EXEC sp_msforeachtable @command1="print '?'", 
                       @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all";

禁用约束:

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all";

Try truncate table command like below :

TRUNCATE TABLE tablename

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:

EXEC sp_msforeachtable @command1="print '?'", 
                       @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all";

To Disable constraint:

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文