我已经将记录插入到SQL Server数据库表中。该表定义了一个主键,并且自动递增标识种子被设置为“Yes”。这样做主要是因为在SQL Azure中,每个表都必须定义一个主键和标识。

但是由于我必须从表中删除一些记录,这些表的标识种子将受到干扰,索引列(自动生成的增量为1)也将受到干扰。

如何在删除记录后重置标识列,使该列具有升序数字顺序?

标识列在数据库中的任何地方都不能用作外键。


当前回答

@jacob

DBCC CHECKIDENT ('[TestTable]', RESEED,0)
DBCC CHECKIDENT ('[TestTable]', RESEED)

对我来说,我只需要先从表中清除所有条目,然后在删除后的触发点中添加上面的条目。现在每当我删除一个条目时,它就从那里被取出。

其他回答

重新播种到0是不太实际的,除非您要清理整个表。

除此之外,安东尼·雷蒙德给出的答案是完美的。首先得到单位列的最大值,然后用max作为种子。

这里的大多数回复似乎都假定表为空,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,它似乎值得添加。

在可能的情况下使用TRUNCATE总是比删除所有记录更好,因为它也不使用日志空间。

如果我们需要删除和重置种子,请记住,如果表从未被填充,并且您使用DBCC CHECKIDENT('tablenem',RESEED,0) 那么第一条记录将得到identity = 0 如MSDN文档所述

在您的情况下,只重建索引,而不用担心丢失 级数恒等式这样的情况很常见。

这是一个常见的问题,答案总是一样的:不要这样做。身份值应该被视为任意的,因此,没有“正确”的顺序。

你可以CHECKIDENT重置种子

DBCC CHECKIDENT
 (
    table_name
        [ , { NORESEED | { RESEED [ , new_reseed_value ] } } ]
)
[ WITH NO_INFOMSGS ]

例子

 DBCC CHECKIDENT ('TAble', reseed,0)

-- 示例查询

您可以用以下代码插入基本数据

向表中插入数据后,先创建一个表

步骤到步骤i显示数据和删除数据显示细节理解代码

结果代码:创建带有rest种子Id的表 我用的是车管局系统。Identity_columns表具有标识


--Create Table 
DROP TABLE IF EXISTS  ExampleTable
create table ExampleTable (Id Bigint identity(1,1), Name nvarchar(10))

--Insert to ExampleTable and Delete and Show identity 
insert into ExampleTable (Name) 
select 'Test1' as NAme union all select 'Test2' as NAme

select * from ExampleTable

| Id       | Name |
| -------- | -----|
| 1        |Test1 |
| 2        |Test2 |

delete from ExampleTable

insert into ExampleTable (Name) select 'Test3' as NAme

select * from ExampleTable

| Id       | Name |
| -------- | -----|
| 3        |Test3 |

delete from ExampleTable

首次检查数据 如果表中没有数据使用种子表

如果表中有数据,使用最大id

之后用CHECKIDENT改变种子

--Find seedTable
declare @reseed int=0

if(not exists( select top 1 * from ExampleTable))
begin

    
     SELECT 
        @reseed=cast( seed_value as int)
    FROM sys.tables tables 
        JOIN sys.identity_columns identity_columns 
    ON tables.object_id=identity_columns.object_id
    where 
        tables.name='ExampleTable' 
    and OBJECT_SCHEMA_NAME(tables.object_id, db_id())='dbo'
 
      set @reseed=@reseed -1

 end
 else
 begin
   --if Table Has Data and use Max id For  seed
    set @reseed=(select top 1 id from ExampleTable order by id desc)

 end


  DBCC CHECKIDENT ('ExampleTable', reseed,@reseed)


insert into ExampleTable
(Name)
select 'Test4' as NAme


select * from ExampleTable


| Id       | Name |
| -------- | -----|
| 1        |Test4 |
 

GO