SQL Server中自增主键的上限

发布于 2024-07-08 00:45:10 字数 64 浏览 4 评论 0原文

SQL Server中自动增量主键的上限是多少? 当 SQL Server 自动增量主键达到上限时会发生什么?

What is the upper limit for an autoincrement primary key in SQL Server?
What happens when an SQL Server autoincrement primary key reaches its upper limit?

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

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

发布评论

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

评论(5

眼角的笑意。 2024-07-15 00:45:10

乔尔的答案是正确的,它是您使用的任何数据类型的上限。

下面是其中两个的示例:

  • int: 2^31-1 (2,147,483,647)
  • bigint: 2^63-1 (9,223,372,036,854,775,807)

我实际上已经达到了我所从事的工作的极限。 实际的错误是:

    Msg 8115, Level 16, State 1, Line 1
    Arithmetic overflow error converting IDENTITY to data type int.
    Arithmetic overflow occurred.

我立即想到了一些修复方法。 第 1 点可能非常困难,而且不太可能,第 2 点很简单,但可能会在您的代码库中引起问题。

  1. 如果标识列对您来说并不重要(它不是外键等),那么您只需重新播种数据库并重置标识列即可。
  2. 将您的身份栏更改为更大的数字。 因此,例如,如果您溢出了一个 int,请将您的标识列更改为一个大 int。 祝你好运:)

可能还有其他修复方法,但没有简单的灵丹妙药。 我只是希望这种情况不会发生在作为一堆关系中心的表中,因为如果发生了,你就会遭受很多痛苦。 这不是一个困难的修复,只是一个乏味而漫长的修复。

Joel's answer is correct, it is the upper limit of whatever datatype you use.

Here's an example of two of them:

  • int: 2^31-1 (2,147,483,647)
  • bigint: 2^63-1 (9,223,372,036,854,775,807)

I have actually hit the limit at a job I worked at. The actual error is:

    Msg 8115, Level 16, State 1, Line 1
    Arithmetic overflow error converting IDENTITY to data type int.
    Arithmetic overflow occurred.

There are a couple fixes to this I can think of off the top of my head. Number 1 is probably very hard and not very likely, number 2 is easy, but will probably cause problems in your code base.

  1. If the identity column doesn't matter to you (it's not a Foreign Key, etc.) then you can just reseed the database and reset the identity column.
  2. Change your identity column to a bigger number. So for example if you've overflowed an int, change your identity column to a big int. Good luck overflowing that :)

There are probably other fixes, but there is no magic bullet easy one. I just hope this doesn't happen in a table that is the center of a bunch of relationships, because if it does, you're in for a lot of pain. It's not a hard fix, just a tedious and long one.

三生路 2024-07-15 00:45:10

这取决于数据类型。 如果您使用 bigint,则不太可能发生溢出。 即使一个普通的 int 也会给你几十亿行。 我从来没有溢出过,所以我无法告诉你如果溢出会发生什么。

It depends on the datatype. If you use bigint, you're unlikely to ever overflow. Even a normal int gives you a couple billion rows. I've never overflowed, so I can't tell you what happens if you do.

橘虞初梦 2024-07-15 00:45:10

我会告诉你发生了什么......我的数据停止插入到那个特定的表中。 数据库仍然有效,但我发现数据丢失且不一致。 经过一些研究,我找到了错误表,然后运行了手动插入。 错误与上面相同。

必须将该列更改为 BIGINT。 在速度较慢的服务器上的 26GB 数据库上,大约需要 30 分钟。 在数据库的存档版本(150GB 左右)上,花费的时间相当长。

幸运的是,这张表没有太多关系,所以痛苦相当轻微。

I'll tell you what happens.... my data stopped inserting into that specific table. The database still works but I found data missing and inconsistent. With a little research, I found the error table, then ran a manual insert. The error is the same as above.

Had to change the column to BIGINT. On a 26GB database on a somewhat slow server, took about 30 minutes. On the archive version of the database (150GB or so) it took quite a bit longer.

Fortunately, not too many relationships for this table so the pain was pretty slight.

白馒头 2024-07-15 00:45:10

DBCC CHECKIDENT (SomeTable, RESEED, 1)

这会将表“SomeTable”上的标识重置为 1

不确定这是否是执行此操作的最佳方法。

DBCC CHECKIDENT (SomeTable, RESEED, 1)

This resets the identity to 1 on table 'SomeTable'

Not sure if this is the best way to do this.

清风挽心 2024-07-15 00:45:10

数据类型说明:

BIGINT    Integer data from -2^63 through 2^63 - 1

INT       Integer data from -2^31 through 2^31 - 1

SMALLINT  Integer data from -2^15 through 2^15 - 1

TINYINT   Integer data from 0     through 255

当达到上限时,自动增量将转到下限。

Data types descriptions:

BIGINT    Integer data from -2^63 through 2^63 - 1

INT       Integer data from -2^31 through 2^31 - 1

SMALLINT  Integer data from -2^15 through 2^15 - 1

TINYINT   Integer data from 0     through 255

When you reach the upper limit the autoincrement goes to the lower limit.

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