数据库教程:SQL Server序列SEQUENCE用法介绍

一、概述sql server2012 之前版本,一般采用guid或者identity来作为标示符。在2012中,微软终于增加了 sequence 对象,功能和性能都有了很大的提高。序列是一种用户定义的

一、概述

sql server2012 之前版本,一般采用guid或者identity来作为标示符。在2012中,微软终于增加了 sequence 对象,功能和性能都有了很大的提高。 
序列是一种用户定义的架构绑定对象,它根据创建该序列时采用的规范生成一组数值。 这组数值以定义的间隔按升序或降序生成,并且可根据要求循环(重复)。

  • 序列不与表相关联,这一点与标识列不同。
  • 应用程序将引用某一序列对象以便接收其下一个值。
  • 序列是通过使用 create sequence 语句独立于表来创建的。 其选项使您可以控制增量、最大值和最小值、起始点、自动重新开始功能和缓存以便改进性能。
  • 与在插入行时生成的标识列值不同,应用程序可以通过调用 next value for 函数在插入行之前获取下一序列号。 在调用 next value for 时分配该序列号,即使在该序列号永远也不插入某个表中时也是如此。 此 next value for 函数可用作表定义中某个列的默认值。
  • 使用 sp_sequence_get_range 可一次获取某个范围的多个序列号。
  • 序列可定义为任何整数数据类型。 如tinyint, smallint, int, bigint, decimal 或是小数精度为0的数值类型。如果未指定数据类型,则序列将默认为 bigint

序列的限制(limitation)有二个

  • 序列不支持事务,即使事务中进行了回滚(rollback)操作,序列仍然返回下一个元素。
  • 序列不支持sql server 复制(replication),序列不会复制到订阅的sql server实例中。如果一个表的默认值依赖一个序列,而序列又是不可复制的,这会导致订阅的sql server出现脚本错误。

选择使用序列的情况:

在以下情况下将使用序列,而非标识列:

  • 应用程序要求在插入到表中之前有一个数值。
  • 应用程序要求在多个表之间或者某个表内的多个列之间共享单个数值系列。
  • 在达到指定的数值时,应用程序必须重新开始该数值系列。 例如,在分配值 1 到 10 后,应用程序再次开始分配值 1 到 10。
  • 应用程序要求序列值按其他字段排序。 next value for 函数可以将 over 子句应用于该函数调用。 over 子句确保返回的值按照 over 子句的 order by 子句的顺序生成。
  • 应用程序要求同时分配多个数值。 例如,应用程序需要保留五个序号。 如果正在同时向其他进程发出数值,则请求标识值可能会导致在系列中出现间断。 调用 sp_sequence_get_range 可以一次检索该序列中的若干数值。
  • 您需要更改序列的规范,例如增量值。

二、创建序列:create sequence

我们可以在ssms中创建也可以使用sql server脚本创建序列对象:

SQL Server序列SEQUENCE用法介绍

SQL Server序列SEQUENCE用法介绍

1、使用默认值创建序列:若要创建从 -2,147,483,648 到 2,147,483,647 且增量为 1 的整数序列号。

create sequence schema.sequencename        as int        increment by 1 ;

2、若要创建类似于从 1 到 2,147,483,647 且增量为 1 的标识列的整数序列号,请使用以下语句。

create sequence schema.sequencename        as int        start with 1        increment by 1 ;

3、使用所有参数创建序列

以下示例使用 decimal 数据类型(范围为 0 到 255)创建一个名为 decseq 的序列 。 序列以 125 开始,每次生成数字时递增 25。 因为该序列配置为可循环,所以,当值超过最大值 200 时,序列将从最小值 100 重新开始。

create sequence test.decseq        as decimal(3,0)         start with 125        increment by 25        minvalue 100        maxvalue 200        cycle        cache 3  ;

二、使用序列号:next value for

执行以下语句可查看第一个值;start with 选项为 125。将该语句再执行三次,以返回 150、175 和 200。再次执行该语句,以查看起始值如何循环回到 minvalue选项值 100。

select next value for test.decseq;

1、序列值插入到表中

下面的示例创建一个名为 test 的架构、一个名为 orders 的表以及一个名为 countby1 的序列,然后使用 next value for 函数将行插入到该表中。

--create the test schema    create schema test ;    go        -- create a table    create table test.orders        (orderid int primary key,        name varchar(20) not null,        qty int not null);    go        -- create a sequence    create sequence test.countby1        start with 1        increment by 1 ;    go        -- insert three records    insert test.orders (orderid, name, qty)        values (next value for test.countby1, 'tire', 2) ;    insert test.orders (orderid, name, qty)        values (next value for test.countby1, 'seat', 1) ;    insert test.orders (orderid, name, qty)        values (next value for test.countby1, 'brake', 1) ;    go        -- view the table    select * from test.orders ;    go

下面是结果集:

orderid name qty

1 tire 2

2 seat 1

3 brake 1

2、在select 语句中使用 next value for 。

select next value for countby5 as surveygroup, name from sys.objects ;

3、通过使用 over 子句为结果集生成序列号

select next value for samples.idlabel over (order by name) as nutid, productid, name, productnumber from production.product    where name like '%nut%' ;

4、sp_sequence_get_range:同时获取多个序列号

从序列对象中返回一系列序列值。 序列对象生成和发出请求的值数目,并为应用程序提供与该系列序列值相关的元数据。

以下语句从 rangeseq 序列对象中获取四个序列号,并向用户返回过程中的所有输出值。

declare @range_first_value_output sql_variant ;        exec sys.sp_sequence_get_range    @sequence_name = n'test.rangeseq'    , @range_size = 4    , @range_first_value = @range_first_value_output output ;        select @range_first_value_output as firstnumber ;

5、将表从标识更改为序列

下面的示例创建一个包含该示例的三行的架构和表。 然后,该示例添加一个新列并且删除旧列。

使用 transact-sql 的 select * 语句将这个新列作为最后一列接收,而非作为第一列接收。 如果这样做是不可接受的,则您必须创建全新的表,将数据移到该表中,然后针对这个新表重新创建权限。

-- 添加没有identity属性的新列  alter table test.department         add departmentidnew smallint null    go        -- 将值从旧列复制到新列    update test.department        set departmentidnew = departmentid ;    go        -- 删除旧列上的主键约束    alter table test.department        drop constraint [pk_department_departmentid];    -- 删除旧列    alter table test.department        drop column departmentid ;    go        -- 将新列重命名为旧列名    exec sp_rename 'test.department.departmentidnew',         'departmentid', 'column';    go        -- 将新列更改为not null    alter table test.department        alter column departmentid smallint not null ;    -- 添加唯一的主键约束    alter table test.department        add constraint pk_department_departmentid primary key clustered              (departmentid asc) ;    -- 从departmentid列中获取当前的最高值,并创建一个用于列的序列。(返回3。)   select max(departmentid) from test.department ;    --使用下一个期望值(4)作为start with value;    create sequence test.deptseq        as smallint        start with 4        increment by 1 ;    go        -- 为departmentid列添加一个默认值    alter table test.department        add constraint defsequence default (next value for test.deptseq)   for departmentid;    go        -- 查看结果    select departmentid, name, groupname  from test.department ;     -- test insert    insert test.department (name, groupname)  values ('audit', 'quality assurance') ;    go        -- 查看结果    select departmentid, name, groupname  from test.department ;    go

三、管理序列

1、更新(重置)序列:alter sequence

重新开始 samples.idlabel 序列。

alter sequence samples.idlabel  restart with 1 ;

2、drop sequence:删除序列

在生成编号后,序列对象与其生成的编号之间没有延续关系,因此可以删除序列对象,即使生成的编号仍在使用。

当序列对象由存储过程或触发器引用时,可以删除序列对象,因为序列对象未绑定到架构上。 如果序列对象是作为表中的默认值引用的,则无法删除序列对象。 错误消息将列出引用序列的对象。

以下示例从当前数据库中删除一个名为 countby1 的序列对象。

drop sequence countby1 ;

3、查看序列信息

有关序列的信息,请查询 sys.sequences。

执行以下代码,以确认缓存大小并查看当前值。

select cache_size, current_value   from sys.sequences  where name = 'decseq' ;

到此这篇关于sql server序列sequence的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

需要了解更多数据库技术:SQL Server序列SEQUENCE用法介绍,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/1238423.html

(0)
上一篇 2022年9月11日
下一篇 2022年9月11日

精彩推荐