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;
GO
USE 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 INT
START WITH 1
INCREMENT 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 INT
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 3
CYCLE
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 INT
START WITH 1
INCREMENT BY 1
MAXVALUE 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序列