当主键 Id 超过限制时会发生什么? [英] What happens to the primary key Id when it goes over the limit?

查看:40
本文介绍了当主键 Id 超过限制时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果添加一条新记录,然后删除该记录,然后一遍又一遍地添加,及时,迟早,当您添加新记录的整数主键id时,它最终会超过20亿.

If you add a new record, then delete the record, then add it again over and over, in time, sooner or later, when you add a new record the integer primary key id, it will eventually exceed 2 billion.

  1. 现在会发生什么?SQL Server 会再次从 1 开始主键 id 吗?还是-1?

  1. Now what happens? SQL Server will start the primary key id from 1 again? or -1?

如果循环 40 亿次会发生什么;SQL Server 怎么知道不替换之前的数据?

What happens if it cycles 4 billion times; how does SQL Server know not to replace the previous data?

推荐答案

如果 identity 超出数据类型的界限,您的问题的其余部分将变得毫无意义,您会收到错误消息.你可以看到这个

You get an error if the identity would exceed the bounds of the datatype making the rest of your question moot. You can see this by

CREATE TABLE #T
(
id INT IDENTITY(2147483647,1)
)

INSERT INTO #T
DEFAULT VALUES

INSERT INTO #T
DEFAULT VALUES /*Arithmetic overflow error converting IDENTITY to data type int.*/

GO

SELECT * FROM #T

DROP TABLE #T

这篇关于当主键 Id 超过限制时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆