SQL Server中自增主键的上限
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
乔尔的答案是正确的,它是您使用的任何数据类型的上限。
下面是其中两个的示例:
我实际上已经达到了我所从事的工作的极限。 实际的错误是:
我立即想到了一些修复方法。 第 1 点可能非常困难,而且不太可能,第 2 点很简单,但可能会在您的代码库中引起问题。
可能还有其他修复方法,但没有简单的灵丹妙药。 我只是希望这种情况不会发生在作为一堆关系中心的表中,因为如果发生了,你就会遭受很多痛苦。 这不是一个困难的修复,只是一个乏味而漫长的修复。
Joel's answer is correct, it is the upper limit of whatever datatype you use.
Here's an example of two of them:
I have actually hit the limit at a job I worked at. The actual error is:
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.
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.
这取决于数据类型。 如果您使用 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.
我会告诉你发生了什么......我的数据停止插入到那个特定的表中。 数据库仍然有效,但我发现数据丢失且不一致。 经过一些研究,我找到了错误表,然后运行了手动插入。 错误与上面相同。
必须将该列更改为 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.
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.
数据类型说明:
当达到上限时,自动增量将转到下限。
Data types descriptions:
When you reach the upper limit the autoincrement goes to the lower limit.