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

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

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

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


当前回答

需要注意的是,如果所有的数据都是通过DELETE从表中删除的(即没有WHERE子句),那么只要a)权限允许,b)没有fk引用表(这里似乎就是这种情况),使用TRUNCATE table将是首选,因为它可以更有效地DELETE并同时重置IDENTITY种子。以下细节取自TRUNCATE TABLE的MSDN页面:

Compared to the DELETE statement, TRUNCATE TABLE has the following advantages: Less transaction log space is used. The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log. Fewer locks are typically used. When the DELETE statement is executed using a row lock, each row in the table is locked for deletion. TRUNCATE TABLE always locks the table (including a schema (SCH-M) lock) and page but not each row. Without exception, zero pages are left in the table. After a DELETE statement is executed, the table can still contain empty pages. For example, empty pages in a heap cannot be deallocated without at least an exclusive (LCK_M_X) table lock. If the delete operation does not use a table lock, the table (heap) will contain many empty pages. For indexes, the delete operation can leave empty pages behind, although these pages will be deallocated quickly by a background cleanup process. If the table contains an identity column, the counter for that column is reset to the seed value defined for the column. If no seed was defined, the default value 1 is used. To retain the identity counter, use DELETE instead.

下面是:

DELETE FROM [MyTable];
DBCC CHECKIDENT ('[MyTable]', RESEED, 0);

变成:

TRUNCATE TABLE [MyTable];

请参阅TRUNCATE TABLE文档(上面有链接)以获得有关限制等的其他信息。

其他回答

你可以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

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

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

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

我尝试了@anil shahs的答案,它重置了身份。但是当插入新的行时,它得到了恒等式= 2。所以我把语法改为:

DELETE FROM [TestTable]

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

那么第一行将得到恒等式= 1。

截断表是首选,因为它可以清除记录,重置计数器并回收磁盘空间。

Delete和CheckIdent应该只在外键阻止截断的情况下使用。

DBCC CHECKIDENT (<TableName>, reseed, 0)

这将把当前标识值设置为0。

在插入下一个值时,标识值将增加到1。