简体   繁体   English

IDENTITY不生成种子并正确递增

[英]IDENTITY not generating the seed and increment correctly

I set my column ID as IDENTITY with seed and increment = 1,1. 我将列ID设置为IDENTITY with seed且increment = 1,1。 But, it does not start counting from 1. Instead it starts at 2. When I insert the next row, it sets the ID = 7 and not 2. Why could this be happening and how do I fix it ? 但是,它不会从1开始计数。而是从2开始。当我插入下一行时,它设置ID = 7而不是2.为什么会发生这种情况,我该如何解决?

I scripted the table and also checked management studio. 我编写了表格并检查了管理工作室。 It is actually an IDENTITY(1,1). 它实际上是一个IDENTITY(1,1)。

IDENTITY does not guarantee a contiguous set of values with no gaps. IDENTITY不保证一组连续的值没有间隙。 If this is what you need, you should consider something else (eg rolling your own serializable MAX+1 solution). 如果这是您所需要的,您应该考虑其他事项(例如,滚动您自己的可序列化MAX+1解决方案)。 All kinds of things can create gaps: 各种事物都可能造成差距:

  • an INSERT that fails INSERT失败
  • a transaction that is rolled back 回滚的事务
  • this bug 这个bug
  • a subsequent DELETE (which can also ruin your own solution too) 随后的DELETE(也可能破坏你自己的解决方案)
  • etc. etc. 等等

It's by Design. 这是设计。 There is NO GUARANTEE of consecutive-ness for IDENTITY Column. IDENTITY列没有连续保证。

See the response from Microsoft to this "Bug" Report: Identity Column jumps by Seed value . 请参阅Microsoft对此“Bug”报告的响应: Identity Column按种子值跳转

Posted by Microsoft on 1/25/2013 at 3:38 PM 微软于2013年1月25日下午3:38发布

Thanks for your feedback. 感谢您的反馈意见。 This behavior is actually by design as we try to guarantee uniqueness of the ID rather than making sure we don't have gaps. 这种行为实际上是设计的,因为我们试图保证ID的唯一性,而不是确保我们没有差距。 As a result, we do skip some values just in case in certain scenarios so that we don't have any issues around accidentally repeating numbers. 因此,我们会在某些情况下跳过某些值,以便我们在偶然重复数字时没有任何问题。

Even if the table is empty, SQL server will remember the last identity it used. 即使表是空的,SQL服务器也会记住它使用的最后一个标识。 If you want to reset it back to 1 try this: 如果要将其重置为1,请尝试以下操作:

DBCC CHECKIDENT('Customer', RESEED, 0)
CREATE TABLE Id_Table (
        [Id] int IDENTITY(1,1), Value INT);
GO

INSERT INTO Id_Table (Value)
VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
GO

DELETE FROM Id_Table
WHERE ID = 1 OR  ID = 3 OR ID = 5 OR ID >7
GO

SELECT * FROM Id_Table

DBCC CHECKIDENT('Id_Table', RESEED, 0)  --<-- Reseed to any smallest number
DBCC CHECKIDENT('Id_Table', RESEED)     --<-- Reseed without providing any seed value

SQL SERVER Message SQL SERVER消息

Checking identity information: current identity value '10', current column value '0'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Checking identity information: current identity value '0', current column value '7'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Reseed the table to Zero and then just execute the DBCC command without any seed value and it will reseed the Identity value to next highest available Identity value. 将表重新设置为Zero,然后只执行不带任何种子值的DBCC命令,它会将Identity值重新设置为下一个最高可用标识值。

Actually it's pretty simple to resolve this problem. 实际上解决这个问题非常简单。 I am using the Visual Studio 2013 designer, SQL Server Object Explorer. 我使用的是Visual Studio 2013设计器,SQL Server对象资源管理器。 Bring up the offending table in Design View, select the Identify field and pull up the properties. 在“设计视图”中打开违规表,选择“识别”字段并拉出属性。 I merely updated the Identity Seed and Increment values and then clicked the Update button. 我只更新了Identity Seed和Increment值,然后单击Update按钮。 This fixed it for me. 这为我修好了。

While Identity increments aren't guaranteed, we have come to expect that they should add 1 to highest increment entered thus far. 虽然不保证身份增量,但我们已经预计到目前为止输入的最高增量应该加1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM