描述 (Description)

SQL Identity columns provide a convenient way to auto-number an ID column within a table without the need to manage the sequence. This convenience can save immense amounts of time, but also presents a single challenge: What happens when an identity column runs out of space within the data type chosen?

SQL Identity列提供了一种方便的方法来自动编号表中的ID列,而无需管理序列。 这种便利可以节省大量时间,但是也带来了一个挑战:当标识列在所选数据类型内的空间不足时会发生什么?

In this article, we will answer this question, as well as explore a variety of ways to detect this problem before it arises and solve it without causing disruption to important tables.

在本文中,我们将回答这个问题,并探索各种方法以在出现问题之前对其进行检测并解决该问题,而不会对重要表造成干扰。

介绍 (Introduction)

SQL Identity columns are often used as a way to auto-number some data element when we have no need to assign any specific values to it. Either the values are arbitrary, the column is a surrogate key, or we wish to generate numbers for use in other processes downstream.

当不需要为数据元素分配任何特定值时,SQL Identity列通常用作自动编号某些数据元素的方法。 值是任意的,列是代理键,或者我们希望生成数字以供下游的其他过程使用。

For most applications, identity columns are set-it-and-forget-it. For an integer, which can contain 2,147,483,647 positive values, it’s easy to assume that a number so large is out-of-reach for a fledgling table. As time goes on, though, and applications grow larger, two billion can quickly seem like a much smaller number. It’s important to understand our data usage, predict identity consumption over time, and proactively manage data type changes before an emergency arises.

对于大多数应用程序,标识列是设置后忘记的。 对于一个可以包含2,147,483,647个正值的整数,很容易假设这么大的数字对于新创建的表是不可行的。 但是,随着时间的流逝,应用程序越来越大,很快就会有20亿个数字变得很小。 重要的是要了解我们的数据使用情况,预测一段时间内的身份消耗,并在紧急情况发生之前主动管理数据类型的更改。

SQL标识列用尽时会发生什么 (What happens when a SQL Identity Column is exhausted)

When a SQL identity column reaches its limit, all insert operations will fail. We can test this easily by creating a table with an identity column, reseeding it to its limit, and then trying to insert a new row. Here’s a table with only 2 columns, an identity that is set to a seed near its maximum value and a string:

当SQL标识列达到其限制时,所有插入操作将失败。 我们可以通过创建一个带有标识列的表,将其重新设置到其极限,然后尝试插入新行来轻松测试。 这是一个只有两列的表,一个身份设置为接近其最大值的种子和一个字符串:

CREATE TABLE dbo.Identity_Test
(   My_Identity INT NOT NULL IDENTITY(2147483646,1) CONSTRAINT PK_Identity_Test PRIMARY KEY CLUSTERED,My_Dinosaur VARCHAR(25) NOT NULL  );

With the table created, we can begin inserting some data:

创建表后,我们可以开始插入一些数据:


INSERT INTO dbo.Identity_Test(My_Dinosaur)
VALUES('Euoplocephalus');
INSERT INTO dbo.Identity_Test(My_Dinosaur)
VALUES('Triceratops');

After these inserts, we can view the data in our table:

这些插入之后,我们可以查看表中的数据:

Note that the identity value for our Triceratops is at the highest allowed by an integer data type. Now, let’s insert one more row:

请注意,三角龙的标识值是整数数据类型所允许的最高值。 现在,让我们再插入一行:

INSERT INTO dbo.Identity_Test(My_Dinosaur)
VALUES('Micropachycephalosaurus');

The result of this SQL INSERT statement is the following error:

此SQL INSERT语句的结果是以下错误:

SQL Server provides no built-in warning. When we exceed the bounds of a data type, we receive the same error that would be returned if we tried to store a higher number in the INTEGER. Until an action is taken on our part to resolve the limit we have hit, inserts will continue to fail. All other operations on this table will execute normally, including DELETE, UPDATE, and SELECT.

SQL Server不提供内置警告。 当我们超出数据类型的界限时,我们收到与试图将更大的数字存储在INTEGER中时将返回的相同错误。 在我们采取行动解决我们遇到的限制之前,插入将继续失败。 该表上的所有其他操作将正常执行,包括DELETE,UPDATE和SELECT。

The remainder of this article will deal with detecting a data type that is getting full with enough time so that we can take an action that does not need to be based on panic

如何解决SQL Server中SQL身份危机相关推荐

  1. SQL Server 中的身份认证讲解

    SQL Server 中的身份认证讲解TIME:2009-4-22   |   READ:228 Microsoft® SQL Server™ 可以在两种安全(身份验证)模式之一下工作: Window ...

  2. sql ddl中key_SQL DDL:SQL Server中SQL DDL命令入门

    sql ddl中key This article explains SQL DDL commands in Microsoft SQL Server using a few simple exampl ...

  3. SQL Server中SQL Union vs Union All

    This article explains to the SQL Union and vs Union All operators in SQL Server. We will also explor ...

  4. 整理:sql server 中sql语句执行顺序

    原文地址为: 整理:sql server 中sql语句执行顺序 SQL Server 查询处理中的各个阶段(SQL执行顺序) SQL 不同于与其他编程语言的最明显特征是处理代码的顺序.在大数编程语言中 ...

  5. SQL Server 中SQL语句执行顺序

    SQL Server 中SQL语句执行顺序 我们需要对SQL语句的执行顺序了若指掌,才能更好的理解SQL. SQL 不同于与其他编程语言的最明显特征是处理代码的顺序. 在大数编程语言中,代码按编码顺序 ...

  6. SQL Server中的身份功能教程

    This article explores the Identity function in SQL Server with examples and differences between thes ...

  7. 替换SQL Server中Windows身份验证登陆名buildin\Administrator

    以Administrator账户登陆服务器的人员,均可以以buildin\Administrator登陆数据库,并且拥有sysadmin最大权限.而能够使用Administrator登陆服务器可能包括 ...

  8. SQL server 中SQL语句实战操作

    学习网址链接: https://www.w3school.com.cn/sql/sql_top.asp 学习案例链接: https://wenku.baidu.com/view/720053b459f ...

  9. SQL Server中SQL是什么?

    含义和定义 (Meaning and definition) SQL stands for Structured Query Language, a language for manipulating ...

最新文章

  1. C指针原理(3)-ATT汇编
  2. 怎么设计一个好的数据库
  3. 宁波中小学生计算机技术展示,2020年宁波市中小学生电脑制作活动创客竞赛暨2020年宁波市中小学生创客大赛顺利举行...
  4. 你怕是对MD5算法有误解
  5. 从Gradle自动将工件提升到Maven Central
  6. python for循环连续输入五个成绩判断等级_Python条件循环判断
  7. uml+oopc嵌入式c语言开发精讲_嵌入式开发中更接近底层的汇编与C语言
  8. mysql 添加表索引_如何向MySQL表中添加索引?
  9. mac python版本问题
  10. 图解 利用vue-cli 脚手架创建项目
  11. 暮光之城3蓝光BD高清下载
  12. 织梦php 文章采集规则,dede自带采集器的高阶技巧
  13. 每日一题之 hiho1542 无根树变有根树
  14. 计算机管理中优盘显示无媒体,无法识别、无媒体、无容量等的U盘,是怎么造成的?(故障篇)...
  15. 微信如何做好服务器,如何用免费服务器做微信JS开发
  16. vuex中subscribe的使用
  17. Xcode升级以后构建ios出现 “unable to find utility PackageApplication, not a developer tool or in PATH”的报错解决
  18. [ASP.NET]文件处理
  19. linux系统:rm-rf执行以后,怎么办?我来教你恢复文件
  20. 计算机组成加减交替法被除数,2019考研408计算机组成原理知识:定点数的表示和运算...

热门文章

  1. python 数据清洗 豆瓣电影_Python高阶操作--关于数据清洗
  2. mysql 获取下一条记录数,如何在MySQL中查询当前数据上一条和下一条的记录
  3. 浅谈c#中使用lock的是与非
  4. mongoose数据查询or、and、where等用法
  5. LeetCode(1137)——第 N 个泰波那契数(JavaScript)
  6. LeetCode(653)——两数之和 IV - 输入 BST(JavaScript)
  7. 句句真研—每日长难句打卡Day17
  8. 警告 1 warning C4996: ‘scanf‘: This function or variable may be unsafe.
  9. java代码实现画板_求好心人帮找或做个JAVA画板程序 代码,主要能实现简单的画板功能!...
  10. python登录跳转_Python模拟登录和登录跳转的参考示例