我已经将记录插入到SQL Server数据库表中。该表定义了一个主键,并且自动递增标识种子被设置为“Yes”。这样做主要是因为在SQL Azure中,每个表都必须定义一个主键和标识。
但是由于我必须从表中删除一些记录,这些表的标识种子将受到干扰,索引列(自动生成的增量为1)也将受到干扰。
如何在删除记录后重置标识列,使该列具有升序数字顺序?
标识列在数据库中的任何地方都不能用作外键。
我已经将记录插入到SQL Server数据库表中。该表定义了一个主键,并且自动递增标识种子被设置为“Yes”。这样做主要是因为在SQL Azure中,每个表都必须定义一个主键和标识。
但是由于我必须从表中删除一些记录,这些表的标识种子将受到干扰,索引列(自动生成的增量为1)也将受到干扰。
如何在删除记录后重置标识列,使该列具有升序数字顺序?
标识列在数据库中的任何地方都不能用作外键。
当前回答
这里的大多数回复似乎都假定表为空,Identity值需要重置。 然而,我如何阅读这个问题是@xorpower现在有一个记录1,2,3,5,6,7,12,13,14等的表…并需要一个方法将其返回到一个连续列表。(1、2、3、4、5、6、7、8、9等…)
但是由于我必须从表中删除一些记录, 恕我直言,这里有个神奇的词。
AFAIK在MSSQL中没有这样的东西;在最坏的情况下,您确实可以将现有的记录表转储到一个新表中,然后从那里开始。我的问题是:你为什么要这样做?IDENTITY列是最好的方法吗?
Anyway,the solutions provided here that do care about existing data are mostly about copying everything in a temp-table, TRUNCATEing the existing table; reseeding the table and then copying everything back again. I'm sure that works but if you have a lot of data then this is a pretty heavy operation. Personally I would rather go with creating an identical table, copying the data in that new table (maybe in batches?) and then finally SWITCHing the data to the original table and dropping the newly created table again. You're likely to need to do a CHECKIDENT after the SWITCH. This way you only need to move the data from one table to another once. To save space you could even DELETE the relevant records from the original table after a batch is copied.
PS:是的,我知道这是一个老问题,但考虑到它的高得分,它仍然出现在类似问题的顶部,因为没有人提到SWITCH,它似乎值得添加。
其他回答
我尝试了@anil shahs的答案,它重置了身份。但是当插入新的行时,它得到了恒等式= 2。所以我把语法改为:
DELETE FROM [TestTable]
DBCC CHECKIDENT ('[TestTable]', RESEED, 0)
GO
那么第一行将得到恒等式= 1。
虽然大多数答案都建议RESEED为0,而且有些人认为这是TRUNCATED表的缺陷,但微软有一个排除ID的解决方案
DBCC CHECKIDENT ('[TestTable]', RESEED)
这将检查表并重置到下一个ID。它从MS SQL 2005到现在都是可用的。
https://msdn.microsoft.com/en-us/library/ms176057.aspx
我使用下面的脚本来做到这一点。只有一种情况下,它会产生一个“错误”,即如果你已经删除了表中的所有行,而IDENT_CURRENT当前设置为1,即表中只有一行开始。
DECLARE @maxID int = (SELECT MAX(ID) FROM dbo.Tbl)
;
IF @maxID IS NULL
IF (SELECT IDENT_CURRENT('dbo.Tbl')) > 1
DBCC CHECKIDENT ('dbo.Tbl', RESEED, 0)
ELSE
DBCC CHECKIDENT ('dbo.Tbl', RESEED, 1)
;
ELSE
DBCC CHECKIDENT ('dbo.Tbl', RESEED, @maxID)
;
这里的大多数回复似乎都假定表为空,Identity值需要重置。 然而,我如何阅读这个问题是@xorpower现在有一个记录1,2,3,5,6,7,12,13,14等的表…并需要一个方法将其返回到一个连续列表。(1、2、3、4、5、6、7、8、9等…)
但是由于我必须从表中删除一些记录, 恕我直言,这里有个神奇的词。
AFAIK在MSSQL中没有这样的东西;在最坏的情况下,您确实可以将现有的记录表转储到一个新表中,然后从那里开始。我的问题是:你为什么要这样做?IDENTITY列是最好的方法吗?
Anyway,the solutions provided here that do care about existing data are mostly about copying everything in a temp-table, TRUNCATEing the existing table; reseeding the table and then copying everything back again. I'm sure that works but if you have a lot of data then this is a pretty heavy operation. Personally I would rather go with creating an identical table, copying the data in that new table (maybe in batches?) and then finally SWITCHing the data to the original table and dropping the newly created table again. You're likely to need to do a CHECKIDENT after the SWITCH. This way you only need to move the data from one table to another once. To save space you could even DELETE the relevant records from the original table after a batch is copied.
PS:是的,我知道这是一个老问题,但考虑到它的高得分,它仍然出现在类似问题的顶部,因为没有人提到SWITCH,它似乎值得添加。
发出2号命令就可以做到这一点
DBCC CHECKIDENT ('[TestTable]', RESEED,0)
DBCC CHECKIDENT ('[TestTable]', RESEED)
第一个将标识重置为零,下一个将其设置为下一个可用值 ——雅各