重置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
- I added two rows:
ID=1, ID=2
- I deleted these two rows.
- I added a new row, but the new row's ID was:
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
只是连续输出数字-不会以任何方式否确保没有冲突!如果您已经有值,请不重新设置为更低的值!
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屋!