SQL Identity 列增加 2 或 3

发布于 2024-08-25 22:24:48 字数 230 浏览 3 评论 0原文

我有一个新的集群数据库,并且我一直在将数据从旧数据库移至表中。当我导入数据时,一切正常,但是当我手动插入记录时,“身份”列不会跟随下一个身份号。因此,例如,我将 100 条记录导入表中,“身份”列显示 1-100,但如果我随后手动向数据库添加一行,我将得到 102 或 103,而不是 101。

任何想法为什么会发生这种情况?

我的所有桌子上都发生这种情况,这让人非常沮丧,因为有时身份之间的差距高达 4 或 5。

I have a new clustered database and I've been moving data into the tables from our old database. When I import the data, everything works fine, but when I manually insert a record the Identity column does not follow the next identity number. So, for example, I import 100 records into the table and the Identity column shows 1-100, but if I then manually add a row to the database, I'll get 102 or 103, not 101.

Any ideas why this is happening?

It is happening across all of my tables and it's getting very frustrating as sometimes the gap between Identities is up to 4 or 5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

_蜘蛛 2024-09-01 22:24:48

您不应该依赖遵循任何特定模式的标识列(例如始终只增加 1)。唯一的保证是表中当前的值是唯一的。

You shouldn't depend on identity columns following any particular pattern (e.g. always increasing by only 1). The only guarantee is that the values currently in the table will be unique.

薄凉少年不暖心 2024-09-01 22:24:48

IMO,人们不应该注意身份列中的空白。这种代理键(与自然键相反)的全部要点是不必担心任何类型的“正确性”。

现在,就像罗宾说的那样,您可能有回滚或错误的语句,这会导致身份空白。

另一件可能导致差距的事情是身份本身的定义。运行这个语句看看我的意思

Declare @MyTable table ( MyColumn1 Int Identity (1, 5))

INSERT INTO @MyTable Default Values
INSERT INTO @MyTable Default Values
INSERT INTO @MyTable Default Values
INSERT INTO @MyTable Default Values
INSERT INTO @MyTable Default Values
INSERT INTO @MyTable Default Values

SELECT * FROM @MyTable

IMO, one should not pay attention to gaps in identity columns. The whole point of such a surrogate key (as opposed to a natural key) is to not have to worry about any sort of "correctness".

Now, like Robin said, you may have statements that are rolling back or erroring out which causes gaps in the identity.

The other thing that can cause gaps is the definition of the identity itself. Run this statement and see what I mean

Declare @MyTable table ( MyColumn1 Int Identity (1, 5))

INSERT INTO @MyTable Default Values
INSERT INTO @MyTable Default Values
INSERT INTO @MyTable Default Values
INSERT INTO @MyTable Default Values
INSERT INTO @MyTable Default Values
INSERT INTO @MyTable Default Values

SELECT * FROM @MyTable
七秒鱼° 2024-09-01 22:24:48

如果您在事务和回滚中“测试”插入。然后你就可以真正运行它了。即使您随后回滚,事务中的插入也会将身份字段增加 1。

If you "test" your insert within a transaction and a rollback. And then you run it for real. The insert within the transaction will increment the identity field by one, even if you then rollback.

纵性 2024-09-01 22:24:48

您不能依赖标识列始终是连续的,因为增量由 SQL Server 维护并在事务中保留。尽管在更改表结构或将 SET IDENTITY_INSERT 切换为 ON 并返回为 OFF 时会重置此增量,但我不建议这样做。

如果您确实依赖于顺序 ID,则应该创建一个非标识列并编写自己的逻辑以确保它保持顺序。

You cannot depend on Identity columns to always be sequential, because the increment maintained by SQL Server and is preserved across transactions. Although this increment is reset when altering the table structure or when toggling SET IDENTITY_INSERT to ON and back to OFF, I would not advise it.

If you really depend on sequential IDs, you should create a non-identity column and write your own logic to make sure it stays sequential.

生生不灭 2024-09-01 22:24:48

如果您通过 SSMS 添加行并尝试插入行并遇到任何类型的约束违规(即数据类型违规或外键违规),则即使该行未成功插入到表中,标识也会递增 1。这可能是发生了什么?

If you are adding the row through SSMS and you attempt to insert the row and experience any type of constraint violation (i.e. Datatype Violation or Foreign Key Violation), the identity is incremented by 1 even though the row is not successfully inserted into the table. This may be what is happening?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文