sql server序列

In SQL Server, both the SEQUENCE object and IDENTITY property are used to generate a sequence of numeric values in an ascending order. However, there are several differences between the IDENTITY property and SEQUENCE object. In this article, we will look at these differences.

在SQL Server中,SEQUENCE对象和IDENTITY属性都用于生成一个升序的数字序列。 但是,IDENTITY属性和SEQUENCE对象之间存在一些差异。 在本文中,我们将研究这些差异。

差异1: (Difference 1:)

The IDENTITY property is tied to a particular table and cannot be shared among multiple tables since it is a table column property.

IDENTITY属性绑定到特定表,并且由于是表列属性而无法在多个表之间共享。

On the flip side the SEQUENCE object is defined by the user and can be shared by multiple tables since is it is not tied to any table.

另一方面,SEQUENCE对象由用户定义,并且可以与多个表共享,因为它没有绑定到任何表。

Let’s understand this difference with the help of a simple example.

让我们借助一个简单的示例来了解这种差异。

Execute the following script to create a database and some tables.

执行以下脚本以创建数据库和一些表。

CREATE Database ShowRoom;
GOUSE ShowRoom;
CREATE TABLE Cars1
(id INT PRIMARY KEY IDENTITY(1,1),name VARCHAR(50) NOT NULL,company VARCHAR(50) NOT NULL,power INT NOT NULL)CREATE TABLE Cars2
(id INT,name VARCHAR(50) NOT NULL,company VARCHAR(50) NOT NULL,power INT NOT NULL)CREATE TABLE Cars3
(id INT,name VARCHAR(50) NOT NULL,company VARCHAR(50) NOT NULL,power INT NOT NULL)

In the script above, we create the ShowRoom database with three tables Cars1, Cars2 and Cars3. The table Cars1 has an id column which has an IDENTITY property that starts with a 1 and increments by 1. You can see here that the IDENTITY property is tied to a particular column of a table. This IDENTITY property cannot be shared with other tables for instance the tables Cars2 and Cars3. The other two tables Cars2 and Cars3 do not have any IDENTITY property.

在上面的脚本中,我们使用三个表Cars1,Cars2和Cars3创建ShowRoom数据库。 表格Cars1的id列具有一个IDENTITY属性,该属性以1开头并以1递增。您可以在此处看到IDENTITY属性绑定到一个表的特定列。 此IDENTITY属性不能与其他表共享,例如表Cars2和Cars3。 其他两个表Cars2和Cars3没有任何IDENTITY属性。

Now let’s create a SEQUENCE object, execute the following script:

现在让我们创建一个SEQUENCE对象,执行以下脚本:

CREATE SEQUENCE [dbo].[SequenceCounter]AS INTSTART WITH 1INCREMENT BY 1

In the script above we create a SEQUENCE object namely SequenceCounter. Now let’s share this sequence object between the Cars2 and Cars3 tables.

在上面的脚本中,我们创建一个SEQUENCE对象,即SequenceCounter。 现在,让我们在Cars2和Cars3表之间共享此序列对象。

We will insert some rows into both the tables using the SEQUENCE object we just created. You will see that the SEQUENCE object will be shared between the two tables.

我们将使用刚刚创建的SEQUENCE对象在这两个表中插入一些行。 您将看到SEQUENCE对象将在两个表之间共享。

Let’s first add three records in the Cars2 table:

让我们首先在Cars2表中添加三个记录:

INSERT INTO Cars2 VALUES (NEXT VALUE FOR [dbo].[SequenceCounter], '208', 'Peugeot', 5400)
INSERT INTO Cars2 VALUES (NEXT VALUE FOR [dbo].[SequenceCounter], 'C500', 'BMW', 8000)
INSERT INTO Cars2 VALUES (NEXT VALUE FOR [dbo].[SequenceCounter], 'C500', 'Peugeot', 5400)

You can see that for id column of the Cars2 table we are using a NEXT VALUE FOR clause which will return the next value in the SEQUENCE starting with 1.

您可以看到,对于Cars2表的id列,我们使用了NEXT VALUE FOR子句,该子句将返回SEQUENCE中从1开始的下一个值。

Let’s add three records to Cars3 tables using the same SEQUENCE object. Execute the following script:

让我们使用相同的SEQUENCE对象将三个记录添加到Cars3表中。 执行以下脚本:

INSERT INTO Cars3 VALUES (NEXT VALUE FOR [dbo].[SequenceCounter], 'C500', 'Mercedez', 5000)
INSERT INTO Cars3 VALUES (NEXT VALUE FOR [dbo].[SequenceCounter], 'Prius', 'Toyota', 3200)
INSERT INTO Cars3 VALUES (NEXT VALUE FOR [dbo].[SequenceCounter], 'Civic', 'Honda', 1800)

Now let’s see what is being inserted for the Id column of the Cars2 and Cars3 tables. Execute the following script:

现在,让我们看看为Cars2和Cars3表的ID列插入的内容。 执行以下脚本:

SELECT * FROM Cars2
SELECT * FROM Cars3

The output looks like this:

输出看起来像这样:

You can see that the values for the id column for the Cars1 table are 1, 2, 3 and for Cars3 table the values are 4, 5, 6. Since we are using SEQUENCE object to insert values for the id column and since SEQUENCE object is shared among the tables, therefore the values for id column in Cars3 table are basically continuation of the values in id column of the Cars2 table.

您会看到Cars1表的id列的值是1、2、3,而Cars3表的id列的值是4、5、6。由于我们使用SEQUENCE对象为id列插入值,并且因为SEQUENCE对象在表之间共享,因此Cars3表中id列的值基本上是Cars2表id列中值的延续。

差异2: (Difference 2:)

To generate the next IDENTITY value, a new row has to be inserted into the table. On the other hand, the next VALUE for a SEQUENCE object can simply be generated using the NEXT VALUE FOR clause with the sequence object.

要生成下一个IDENTITY值,必须在表中插入新行。 另一方面,可以简单地使用带有序列对象的NEXT VALUE FOR子句为SEQUENCE对象生成下一个VALUE。

Let’s see this difference in action.

让我们看看这种动作上的差异。

In the ShowRoom database we have a table Cars1 with an IDENTITY property on the id column. To get the next value for the IDENTITY, we have to insert a new row in the Car1 table. Take a look at the following script:

在ShowRoom数据库中,我们有一个表Cars1,其id列上具有IDENTITY属性。 要获得IDENTITY的下一个值,我们必须在Car1表中插入新行。 看一下以下脚本:

INSERT INTO Cars1 VALUES ('Corrolla', 'Toyota', 1800)

The output looks like this:

输出看起来像这样:

You can see that the id column has value of 1. There is no other way to increment the value for the IDENTITY property tied to the id column of Cars1 table, except by inserting a new row in the Cars1 table.

您可以看到id列的值为1。除了在Cars1表中插入新行之外,没有其他方法可以增加与Cars1表的id列关联的IDENTITY属性的值。

On the other hand, the value for a SEQUENCE object can be incremented without inserting a row into a table. Execute the following script:

另一方面,无需在表中插入行就可以增加SEQUENCE对象的值。 执行以下脚本:

SELECT NEXT VALUE FOR [dbo].[SequenceCounter]

The output looks like this:

输出看起来像这样:

You can see that previously, the value for the SequenceCounter SEQUENCE object was 6, now it has been incremented to 7 without inserting a new row to any table.

您可以看到以前,SequenceCounter SEQUENCE对象的值为6,现在已增加到7,而没有在任何表中插入新行。

差异3 (Difference 3)

The value for the IDENTITY property cannot be reset to its initial value. In contrast, the value for the SEQUENCE object can be reset.

IDENTITY属性的值不能重置为其初始值。 相反,可以重置SEQUENCE对象的值。

Take a look at the following script to see how a value can be reset using SEQUENCE object.

看一下以下脚本,看看如何使用SEQUENCE对象重置值。

CREATE SEQUENCE [dbo].[RecycleSequence]AS INTSTART WITH 1INCREMENT BY 1MINVALUE 1MAXVALUE 3CYCLE

To reset the value of a SEQUENCE object, you have to set the minimum and maximum values for the SEQUENCE and have to specify a CYCLE tag with the script. For instance in the above script, the SEQUENCE value is reset 1 once the maximum value i.e. 3 is reached. Therefore, if you execute the following script four times, you will see that 1 will be returned

要重置SEQUENCE对象的值,必须设置SEQUENCE的最小值和最大值,并且必须使用脚本指定CYCLE标记。 例如,在上面的脚本中,一旦达到最大值(即3),就将SEQUENCE值重置为1。 因此,如果您执行以下脚本四次,您将看到将返回1

SELECT NEXT VALUE FOR [dbo].[RecycleSequence]

差异4 (Difference 4)

A maximum value cannot be set for the IDENTITY property. On the other hand, the maximum value for a SEQUENCE object can be defined.

不能为IDENTITY属性设置最大值。 另一方面,可以定义SEQUENCE对象的最大值。

The maximum value that the IDENTITY can take is equal to the maximum value of the data type of the column that the IDENTITY property is tied to. For example, the IDENTITY property of the id column of the Cars1 table can take the maximum value that the INT data type can hold since the type of the id column is INT.

IDENTITY可以采用的最大值等于IDENTITY属性所绑定的列的数据类型的最大值。 例如,由于id列的类型是INT,所以Cars1表的id列的IDENTITY属性可以采用INT数据类型可以保留的最大值。

For a SEQUENCE object the MAXVALUE clause can be used to set the maximum value as shown in the following example.

对于SEQUENCE对象,可以使用MAXVALUE子句设置最大值,如以下示例所示。

CREATE SEQUENCE [dbo].[MaxSequence]AS INTSTART WITH 1INCREMENT BY 1MAXVALUE 3

In the above script, a SEQUENCE object has been created with a maximum value of 3. If you increment the value of this SEQUENCE beyond 3, following error will be thrown:

在上面的脚本中,已创建一个SEQUENCE对象,最大值为3。如果将此SEQUENCE的值增加到3以上,将引发以下错误:

本的其他精彩文章 (Other great articles from Ben)

Sequence Objects in SQL Server
Understanding the GUID data type in SQL Server
Difference between Identity & Sequence in SQL Server
SQL Server中的序列对象
了解SQL Server中的GUID数据类型
SQL Server中身份和序列之间的区别

翻译自: https://www.sqlshack.com/difference-between-identity-sequence-in-sql-server/

sql server序列

sql server序列_SQL Server中身份和序列之间的区别相关推荐

  1. python观察日志(part20)--列表中加号,extend,append之间的区别

    学习笔记,仅供参考,有错必纠 列表中"+"加号,extend,append之间的区别 extend extend函数用于在列表末尾一次性追加另一个序列中的多个值. append a ...

  2. Oracle中用户和架构之间的区别?

    本文翻译自:Difference between a user and a schema in Oracle? Oracle中的用户和架构有什么区别? #1楼 参考:https://stackoom. ...

  3. python __import__和import区别_Python中import 与__import__() 之间的区别比较

    本篇文章给大家带来的内容是关于Python中import 与__import__() 之间的区别比较,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 首先来说一下两者的区别: impo ...

  4. C#中Int64和UInt64之间的区别

    Int64:此 Struct用于表示64位带符号整数.所述的Int64 可以两种类型的值,包括所述范围之间的负的和正的存储-9,223,372,036,854,775,808至9,223,372,03 ...

  5. -ms-flexbox_Flexbox中width和flex-basis之间的区别

    -ms-flexbox by Kyle Gallagher 凯尔·加拉格尔(Kyle Gallagher) Flexbox中width和flex-basis之间的区别 (The difference ...

  6. Jquery中.val()与.value之间的区别

    三年多没敲过代码了,今年打算捡起来,是需要多么大的勇气.但是为了实现自我价值,履行自我的承诺,这就是责任.没有什么难不难,晚不晚之说,是我经常对别人说的那样,再晚不过心晚,,一切努力了,实现了每一天的 ...

  7. c# int uint32_C#中Int32和UInt32之间的区别

    c# int uint32 C#Int32和C#UInt32 (C# Int32 and C# UInt32) In C#, Int32 known as a signed integer of 4 ...

  8. sql server序列_SQL Server中的序列对象功能

    sql server序列 序列介绍 (Introduction to Sequences) 序列是SQL Server 2012中引入的用于密钥生成机制的新对象. 它已在所有版本SQL Server ...

  9. sql server序列_SQL Server中的序列对象

    sql server序列 Sequence objects are used to sequentially generate numeric values. They were introduced ...

最新文章

  1. Nginx负载均衡的详细配置及使用案例
  2. java时间转换为字符串格式错误_字符串转换为日期时间格式及其错误处理(转)
  3. linux系统中怎么驱动U盘
  4. 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
  5. Oracle Database 10g:删除表
  6. django_rest_framework—路由器机制
  7. SAP 全球产品营销总监:产品营销驱动 B2B 企业爆发式增长的 6 大要点
  8. boost::geometry::topological_dimension用法的测试程序
  9. Python函数参数传递:传值还是传引用
  10. 迷宫城堡(HDU-1269)
  11. java 使用apollo,Springboot apollo原理及使用方法详解
  12. Arcgis for android 100.4 getFieldType ()
  13. 数据智能、孪生城市——展望未来智慧城市产业发展
  14. 邮储社招Java笔试题_2019年及历年中国邮政储蓄银行社招笔试题和参考答案6套
  15. 产品设计过程中的沉没成本和禀赋效应
  16. 基于 RTS 超低延时直播优化强互动场景体验
  17. 小白装系统(超详细)
  18. 搜索引擎site关键字的站内搜索
  19. VSLAM与VIO的3D建图,重定位与世界观综述
  20. 东方通应用服务器TongWeb的安装,使用,排错(不定时更新)

热门文章

  1. 项目小记: IFRAME引起内存泄露的解决方法
  2. VS2019中,一个解决方案拥有多个项目,如何快速选择启动项目
  3. [原][osgearth]osgearthElvation中的一帧
  4. ThinkPHP 多语言的实现
  5. 【Unity入门】场景、游戏物体和组件的概念
  6. Kindle 助手上线啦
  7. window.onload 函数不执行处理
  8. sqlhelper 下载 使用指南 代码 [收藏]
  9. C++---基于ffmpeg实现视频播放器(一)
  10. 【零基础学Java】—多线程(四十九)