sql server序列

序列介绍 (Introduction to Sequences)

序列是SQL Server 2012中引入的用于密钥生成机制的新对象。 它已在所有版本SQL Server 2012中提供。它是IDENTITY columns feature that has been prevalent in the previous versions of SQL Server. Despite being newly introduced in SQL Server 2012, sequences have long been prevalent in other database platforms such as IDENTITY列功能的替代功能,该功能在SQL Server的早期版本中已经很普遍。 尽管序列是在SQL Server 2012中新引入的,但是序列在其他数据库平台(如Oracle and Oracle和IBM’s DB2. Thus, the data migration into SQL Server 2012 from other database platforms is now more convenient and simplified.IBM的DB2)中早已盛行。 因此,从其他数据库平台迁移到SQL Server 2012的数据现在更加方便和简化。

身份与序列对象 (IDENTITY vs Sequence Objects)

Sequence objects have been introduced as an alternative to the Identity feature, thus a better way of understanding the usefulness of sequences is by comparing them against the identity feature. Sequences and Identity share lots of similarities but they also differ. Table 1 illustrates some of the feature similarities and differences between sequences and identity.

引入了序列对象作为身份功能的替代方法,因此更好地理解序列有用性的方法是将序列对象与身份功能进行比较。 序列和身份有很多相似之处,但也有所不同。 表1说明了序列和同一性之间的某些特征相似性和差异。

  1. Storage: IDENTITY vs Sequence Objects

    存储:IDENTITY与序列对象

    One major difference between sequences and identity is in the way they are stored in a database. Identity relies on an existence of a table, thus they are stored along the properties of a table. On the other hand, sequences are stored independently of tables. In fact, SQL Server 2012 treats sequences as separate objects.

    序列和同一性之间的主要区别在于它们在数据库中的存储方式。 身份依赖于表的存在,因此它们沿着表的属性存储。 另一方面,序列独立于表存储。 实际上,SQL Server 2012将序列视为单独的对象。

    For example, in SQL Server 2012 Management Studio – there is a separate sub-node of Programmability titled ‘Sequences’ which can be used to view sequences within a given database. In the below image, AdventureWorks2012 has two (2) Sequence objects, namely, dbo.sequenceid as well as HumanResources.sequenceid. Noticeably too in this image is that although the names of these sequences are the same (i.e. sequenceid) they are stored in different schemas – dbo & HumanResources.

    例如,在SQL Server 2012 Management Studio中–有一个单独的可编程性子节点,名为“序列”,可用于查看给定数据库中的序列。 在下图中,AdventureWorks2012具有两(2)个Sequence对象,即dbo.sequenceidHumanResources.sequenceid 。 值得注意的是,尽管这些序列的名称相同(即sequenceid ),但它们存储在不同的架构中-dbo和HumanResources

  2. Generating values: IDENTITY vs Sequence Objects

    生成值:IDENTITY与序列对象

    The sequences are more flexible in the way they generate new values as compared to identity. For instance, while a new increment value for identity is only generated by inserting a new row into a given table, sequences allow for the generating of an increment value outside a table. Thus, Sequences can be generated and stored in a variable which in turn can then be used in an insert statement to append a row into a given table. Furthermore, sequences can be used as unique surrogate keys across multiple tables.

    与标识相比,序列在生成新值的方式上更加灵活。 例如,虽然仅通过在给定表中插入新行来生成用于标识的新增量值,但是序列允许在表外部生成增量值。 因此,可以生成序列并将其存储在变量中,然后可以在插入语句中使用该变量将行追加到给定表中。 此外,序列可用作跨多个表的唯一代理键。

    Sequences, unlike Identity, also have the advantage of generating new increment values during an UPDATE statement. Finally, because sequences can be generated outside an insert statement, their values can be used in multiple columns within a given table.

    与Identity不同,序列还具有在UPDATE语句期间生成新的增量值的优点。 最后,由于可以在插入语句之外生成序列,因此可以在给定表的多个列中使用它们的值。

    Nr Property/Feature Identity Sequence Object
    1 Ability to specify minimum and/or maximum increment values NO YES
    i.e. use MINVALUE, MAXVALUE options in a create or alter sequence object statement
    2 Ability to reset the increment value NO YES
    3 Ability to cache increment value generating NO YES
    4 Ability to specify starting increment value YES YES
    5 Ability to specify increment value YES YES
    r 物业/功能 身分识别 序列对象
    1个 能够指定最小和/或最大增量值 没有
    即在创建或更改序列对象语句中使用MINVALUE,MAXVALUE选项
    2 能够重置增量值 没有
    3 能够缓存增量值生成 没有
    4 能够指定起始增量值
    5 指定增量值的能力

创建序列对象语法 (Create Sequence Object Syntax)

The only mandatory parameter value in a create sequence statement is the sequence name – the rest of the arguments are optional. Thus, the simplest way to create a sequence would be as shown in below:

创建序列语句中唯一必需的参数值是序列名称-其余参数是可选的。 因此,创建序列的最简单方法如下所示:

There is also an option of providing more arguments in your create sequence script. According to Microsoft’s TechNet site, the complete syntax for create sequence is as follows:

还可以选择在创建序列脚本中提供更多参数。 根据Microsoft的TechNet网站,创建序列的完整语法如下:

Noticeable from this above complete script is the number of additional arguments that can be specified. We take a look at these arguments in the below:

从上面的完整脚本中可以注意到,可以指定的其他参数数量。 我们在下面看看这些参数:

  1. AS ArgumentAS参数
    The sequence can either be based in one of the SQL Server 2012 built-in integer data type (i.e.
    该序列可以基于SQL Server 2012内置的整数数据类型之一(即tinyint, smallint, int, bigint, decimal, and/or tinyint,smallint,int,bigint,decimal和/或numeric data type) or it can be based off a SQL Server 2012 user-defined integer data type. If this argument is not specified as part of the create sequence script, then a bigint built-in integer data type is used. 数字数据类型),也可以基于SQL Server 2012用户-定义的整数数据类型。 如果未在创建序列脚本中指定此参数,那么将使用bigint内置的整数数据类型。
  2. START WITH Argument
    Similarly to the seed argument in the IDENTITY property, this argument indicates the initial value to be retrieved by the sequence object.
  3. 争论开始
    与IDENTITY属性中的seed参数相似,此参数指示序列对象要检索的初始值。
  4. INCREMENT BY Argument参数增加
    This is similar to the
    这类似于IDENTITY属性中的increment argument in the IDENTITY property and it refers to the value that is used to increase or decrease the next value of the sequence object. The sequence object can either be in an ascending or descending order. Ascending and descending sequence objects have positive and negative increment values, respectively. If this argument is not specified as part of the create sequence script, then one (1) is used by default as an increment value. 增量参数 ,它引用用于增加或减少序列对象的下一个值的值。 序列对象可以按升序或降序排列。 升序和降序对象分别具有正值和负值。 如果未在创建序列脚本中指定此参数,则默认使用一(1)作为增量值。
  5. MINVALUEAS ArgumentMINVALUEAS参数
    This argument specifies the minimum value in a sequence object. If this argument is not specified as part of the create sequence script, then the minimum value used is that of the data type chosen for a given sequence.
    此参数指定序列对象中的最小值。 如果未在创建序列脚本中指定此参数,则使用的最小值是为给定序列选择的数据类型的最小值。
  6. MAXVALUE ArgumentMAXVALUE参数
    On the contrary to the
    MINVALUE argument, this argument specifies the maximum value in a sequence object. If this argument is not specified as part of the create sequence script, then the maximum value used is that of the data type chosen for a given sequence. MINVALUE参数相反,此参数指定序列对象中的最大值。 如果未在创建序列脚本中指定此参数,则使用的最大值是为给定序列选择的数据类型的最大值。
  7. CYCLE | NO CYCLE Argument
    This argument brings the advantage of sequences over identity by allowing for the resetting of the increment value back to the minimum value upon exceeding of its maximum value. If this argument is not specified as part of the create sequence script, then NO CYCLE is used by default.
  8. 循环| 无周期论点
    通过允许在超过最大值后将增量值重置为最小值,此参数带来了序列优于标识的优点。 如果未在创建序列脚本中指定此参数,则默认使用NO CYCLE。
  9. cache Argument缓存参数
    Another advantage over IDENTITY property, cache refers to the storage in memory of latest sequence value as well as the number remaining values left within a given cache. For instance, for a sequence object that starts at 1 and has a cache size of 20 – values 1 through 20 are stored in memory and made available as next range of increments.
    优于IDENTITY属性的另一个优势是,缓存是指在内存中存储最新序列值以及给定缓存中剩余的剩余数量。 例如,对于从1开始并且缓存大小为20的序列对象,值1到20存储在内存中,并可以用作下一个增量范围。

管理序列对象 (Administering Sequence Objects)

Every database in SQL Server 2012 has a system view titled ‘sys.sequences’ which can be used to retrieve information about sequences of a given database.

SQL Server 2012中的每个数据库都有一个名为“ sys.sequences”的系统视图,可用于检索有关给定数据库序列的信息。

In order to be able to create the sequence object, CREATE SEQUENCE permission is required. CREATE SEQUENCE permission has also been introduced in SQL Server 2012. For more information around this new permission, run the following script:

为了能够创建序列对象,需要CREATE SEQUENCE权限。 SQL Server 2012中还引入了CREATE SEQUENCE权限。有关此新权限的详细信息,请运行以下脚本:


select * from sys.fn_builtin_permissions (DEFAULT) ORDER BY class_desc, permission_name;

序列对象的局限性 (Limitations of Sequence Objects)

Unfortunately, in spite of the aforementioned benefits and flexibility to using sequences when planning for an auto-number generating mechanism, there are limitations to this new SQL Server 2012 feature:

不幸的是,尽管计划了自动编号生成机制时具有上述好处和使用序列的灵活性,但是此新SQL Server 2012功能存在局限性:

  1. SQL Server 2012 database engine specifies its own cache size in instances whereby the cache is enabled but has no cache size. The database engine method used to generate such a cache size is only known to Microsoft and is subject to change without any notice.SQL Server 2012数据库引擎在实例中指定了自己的缓存大小,从而启用了缓存但没有缓存大小。 Microsoft只能使用用于生成这种缓存大小的数据库引擎方法,并且该方法可能会随时更改,恕不另行通知。
  2. Unexpected SQL Server 2012 shutdowns, such as power failures, are likely to cause a loss of sequence numbers remaining in a given sequence cache.SQL Server 2012意外关闭(例如电源故障)可能会导致丢失给定序列缓存中保留的序列号。
  3. Unlike IDENTITY columns, sequence values can be updated which may lead to data integrity. A recommended way of preventing the update of sequences is by rolling-back the changes through an update trigger.与IDENTITY列不同,可以更新序列值,这可能导致数据完整性。 建议的防止序列更新的方法是通过更新触发器回滚更改。
  4. Sequences do not automatically enforce unique values which mean that in a case of a sequence value cycling or updating of statements, duplicate sequence value can be generated. Again, update triggers and unique constraints can be further used to eliminate such a limitation.序列不会自动强制执行唯一值,这意味着在序列值循环或语句更新的情况下,可能会生成重复的序列值。 同样,更新触发器和唯一约束可以进一步用于消除这种限制。
  5. smallint value, a default integer data type for sequence object is smallint值的列中使用的序列,则序列对象的默认整数数据类型为BIGINT.BIGINT

结论 (Conclusion)

The new sequence object introduced in SQL Server 2012 has several advantages over IDENTITY columns. It also makes data migrations from non-Microsoft database platforms very convenient. However, some of the of the benefits such as updating of sequence object values may come at greater cost – such as data integrity and unique constraint violations. Consequently, sequences should never be used as a replacement for IDENTITY columns instead they should be used as alternatives to IDENTITY columns.

与IDENTITY列相比,SQL Server 2012中引入的新序列对象具有多个优点。 这也使从非Microsoft数据库平台进行数据迁移非常方便。 但是,诸如更新序列对象值之类的某些好处可能会付出更高的代价,例如数据完整性和违反唯一约束。 因此,序列不应用作IDENTITY列的替代,而应用作IDENTITY列的替代。

资源: (Resources:)

  • IDENTITY (Property)身份(财产)
  • CREATE SEQUENCE创建序列
  • Sequences顺序
  • CREATE SEQUENCE (Transact-SQL)创建序列(Transact-SQL)

翻译自: https://www.sqlshack.com/sequence-objects-feature-sql-server/

sql server序列

sql server序列_SQL Server中的序列对象功能相关推荐

  1. sql tempdb清理_SQL Server 2019内存优化的TempDB元数据

    sql tempdb清理 In this article, I will walk you through the new feature in SQL Server 2019, memory-opt ...

  2. 使用Excel中的插入对象功能在Excel中插入Word文档

    使用Excel中的插入对象功能,就可以很容易地在Excel中插入Word文档. 具体操作步骤如下: 1.打开要插入Word文档的Excel电子表格文件. 2.单击要插入Word文档的单元格,然后选择菜 ...

  3. word2013插入excel对象报错_使用Excel中的插入对象功能在Excel中插入Word文档

    使用Excel中的插入对象功能在Excel中插入Word文档 时间:2016-05-17   作者:snow   来源:互联网 使用Excel中的插入对象功能,就可以很容易地在Excel中插入Word ...

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

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

  5. sql server序列_SQL Server中的Microsoft时间序列

    sql server序列 The next topic in our Data Mining series is the popular algorithm, Time Series. Since b ...

  6. sql server序列_SQL Server中身份和序列之间的区别

    sql server序列 In SQL Server, both the SEQUENCE object and IDENTITY property are used to generate a se ...

  7. sql 实现决策树_SQL Server中的Microsoft决策树

    sql 实现决策树 Decision trees, one of the very popular data mining algorithm which is the next topic in o ...

  8. sql server 关联_SQL Server中的关联规则挖掘

    sql server 关联 Association Rule Mining in SQL Server is the next article in our data mining article s ...

  9. sql server 群集_SQL Server中的Microsoft群集

    sql server 群集 Microsoft Clustering is the next data mining topic we will be discussing in our SQL Se ...

最新文章

  1. MySQL面试题 | 附答案解析(十)
  2. 一文了解迁移学习经典算法
  3. [原译]11个高效的VS调试技巧
  4. 封神-性能容量分析报告
  5. maven工程servlet实例之jar包冲突解决
  6. java微信内h5调起支付_java微信支付--------公众号内H5调起支付
  7. Oracle中on和where的区别
  8. Python中pip版本升级error:You are using pip version 7.1.2, however version 8.1.1 is available.
  9. python安装后如何使用-python运行环境,python安装后如何使用
  10. windows下的wxWidgets环境配置
  11. 【Android Developers Training】 1. 创建一个Android项目工程
  12. html编辑plist文件,Swift开发:Info.plist文件的常见配置
  13. 信号与线性系统分析(吴大正,郭宝龙)(3-单位脉冲/阶跃序列以及4-信号的运算)
  14. 调用远程摄像头进行人脸识别_工地如何实现安全帽检测/人脸识别?
  15. 原生JS记忆翻牌小游戏
  16. 百度地图使用之基本功能
  17. upc 生命曲线 线段树+lazy
  18. 对物联网的感悟_对物联网产业的理解 对物联网的感悟
  19. LimeSDR srsLTE实验
  20. JAVA有percentile函数吗_五分位算法

热门文章

  1. centos java创建文件_CentOS java生成文件并赋予权限的问题
  2. html实现手机显示效果,手机端分类页面的效果图实现
  3. C#/Net的语法和一些小技巧笔记
  4. adb shell 命令之----pm
  5. 记一次成功部署kolla-ansible ocata版本过程
  6. Shell 概述、截取字符操作等
  7. 【leetcode】Minimum Path Sum
  8. 【js】正则表达式(II)
  9. RN Image与ImageBackground
  10. Mac安装oh-my-zsh更改终端背景