在 Microsoft SQL Server 2008 R2 中重置自动增量 [英] Reset autoincrement in Microsoft SQL Server 2008 R2
问题描述
我创建了一个主键来自动递增.
I created a primary key to be autoincrement.
- 我添加了两行:
ID=1, ID=2
- 我删除了这两行.
- 我添加了一个新行,但新行的 ID 为:
ID=3
如何将自动增量重置或重新启动为 1?
How can I reset or restart the autoincrement to 1?
推荐答案
如果你使用DBCC CHECKIDENT
命令:
If you use the DBCC CHECKIDENT
command:
DBCC CHECKIDENT ("YourTableNameHere", RESEED, 1);
但是小心使用! - 这只会将 IDENTITY
重置为 1 - 所以你的下一个插入将获得值 1,然后是 2,然后是 3 -->并且您将在这里与您先前存在的值 3 发生冲突!
But use with CAUTION! - this will just reset the IDENTITY
to 1 - so your next inserts will get values 1, then 2, and then 3 --> and you'll have a clash with your pre-existing value of 3 here!
IDENTITY
只是按连续顺序排列数字 - 它确实NOT以任何方式确保没有冲突!如果您已经有了值 - 请不要重新设置为较低的值!
IDENTITY
just dishes out numbers in consecutive order - it does NOT in any way make sure there are no conflicts! If you already have values - do not reseed back to a lower value!
这篇关于在 Microsoft SQL Server 2008 R2 中重置自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!