PostgreSQL 序列(SEQUENCE)

PostgreSQL 序列(SEQUENCE),第1张

概述一、简介 序列对象(也叫序列生成器)就是用CREATE SEQUENCE 创建的特殊的单行表。一个序列对象通常用于为行或者表生成唯一的标识符。 二、创建序列 方法一:直接在表中指定字段类型为serial 类型 david=# create table tbl_xulie (david(# id serial,david(# name text);NOTICE: CREATE TABLE w

一、简介

序列对象(也叫序列生成器)就是用CREATE SEQUENCE 创建的特殊的单行表。一个序列对象通常用于为行或者表生成唯一的标识符。

二、创建序列

方法一:直接在表中指定字段类型为serial 类型

davID=# create table tbl_xulIE (davID(# ID serial,davID(# name text);NOTICE:  CREATE table will create implicit sequence "tbl_xulIE_ID_seq" for serial column "tbl_xulIE.ID"CREATE tabledavID=#

方法二:先创建序列名称,然后在新建的表中列属性指定序列就可以了,该列需int 类型

创建序列的语法:

CREATE [ TEMPORARY | TEMP ] SEQUENCE name [ INCREMENT [ BY ] increment ]    [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]    [ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]    [ OWNED BY { table.column | NONE } ]

实例:

davID=# create sequence tbl_xulIE2_ID_seq increment by 1 minvalue 1 no maxvalue start with 1;     CREATE SEQUENCEdavID=# 
davID=# create table tbl_xulIE2 (davID(# ID int4 not null default nextval('tbl_xulIE2_ID_seq'),davID(# name text);CREATE tabledavID=# 

三、查看序列

davID=# \d tbl_xulIE                         table "public.tbl_xulIE" Column |  Type   |                       ModifIErs                        --------+---------+-------------------------------------------------------- ID     | integer | not null default nextval('tbl_xulIE_ID_seq'::regclass) name   | text    | davID=# \d tbl_xulIE2                         table "public.tbl_xulIE2" Column |  Type   |                        ModifIErs                        --------+---------+--------------------------------------------------------- ID     | integer | not null default nextval('tbl_xulIE2_ID_seq'::regclass) name   | text    | davID=#

查看序列属性

davID=# \d tbl_xulIE_ID_seq      Sequence "public.tbl_xulIE_ID_seq"    Column     |  Type   |        Value        ---------------+---------+--------------------- sequence_name | name    | tbl_xulIE_ID_seq last_value    | bigint  | 1 start_value   | bigint  | 1 increment_by  | bigint  | 1 max_value     | bigint  | 9223372036854775807 min_value     | bigint  | 1 cache_value   | bigint  | 1 log_cnt       | bigint  | 0 is_cycled     | boolean | f is_called     | boolean | fOwned by: public.tbl_xulIE.IDdavID=#
davID=# select * from tbl_xulIE2_ID_seq;   sequence_name   | last_value | start_value | increment_by |      max_value      | min_value | cache_value | log_cnt | is_cycled | is_called -------------------+------------+-------------+--------------+---------------------+-----------+-------------+---------+-----------+----------- tbl_xulIE2_ID_seq |          1 |           1 |            1 | 9223372036854775807 |         1 |           1 |       0 | f         | f(1 row)davID=# 

四、序列应用

4.1 在INSERT 命令中使用序列

davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'DavID');      INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Sandy');INSERT 0 1davID=# select * from tbl_xulIE; ID | name  ----+-------  1 | DavID  2 | Sandy(2 rows)davID=# 

4.2 数据迁移后更新序列

davID=# truncate tbl_xulIE;TruncATE tabledavID=# davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Sandy');INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'DavID');INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Eagle');INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Miles');INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Simon');INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Rock'); INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Peter');INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Sally');INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Nicole');INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Monica');INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Renee'); INSERT 0 1davID=# select * from tbl_xulIE; ID |  name  ----+-------- 15 | Sandy 16 | DavID 17 | Eagle 18 | Miles 19 | Simon 20 | Rock 21 | Peter 22 | Sally 23 | Nicole 24 | Monica 25 | Renee(11 rows)davID=# copy tbl_xulIE to '/tmp/tbl_xulIE.sql';copY 11davID=# truncate tbl_xulIE;TruncATE tabledavID=# alter sequence tbl_xulIE_ID_seq restart with 100;ALTER SEQUENCEdavID=# select currval('tbl_xulIE_ID_seq'); currval ---------      25(1 row)davID=# select nextval('tbl_xulIE_ID_seq'); nextval ---------     100(1 row)davID=# select nextval('tbl_xulIE_ID_seq'); nextval ---------     101(1 row)davID=# begin;BEGINdavID=# copy tbl_xulIE from '/tmp/tbl_xulIE.sql';copY 11davID=# select setval('tbl_xulIE_ID_seq',max(ID)) from tbl_xulIE; setval --------     25(1 row)davID=# end;COMMITdavID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Flash');INSERT 0 1davID=# select * from tbl_xulIE; ID |  name  ----+-------- 15 | Sandy 16 | DavID 17 | Eagle 18 | Miles 19 | Simon 20 | Rock 21 | Peter 22 | Sally 23 | Nicole 24 | Monica 25 | Renee 26 | Flash(12 rows)davID=# select nextval('tbl_xulIE_ID_seq'); nextval ---------      27(1 row)davID=#

五、序列函数

下面序列函数,为我们从序列对象中获取最新的序列值提供了简单和并发读取安全的方法。

函数 返回类型 描述
nextval(regclass) bigint 递增序列对象到它的下一个数值并且返回该值。这个动作是自动完成的。即使多个会话并发运行nextval,每个进程也会安全地收到一个唯一的序列值。
currval(regclass) bigint 在当前会话中返回最近一次nextval@H_544_1502@抓到的该序列的数值。(如果在本会话中从未在该序列上调用过nextval@H_544_1502@,那么会报告一个错误。)请注意因为此函数返回一个会话范围的数值,而且也能给出一个可预计的结果,因此可以用于判断其它会话是否执行过nextval。@H_544_1502@
lastval() bigint 返回当前会话里最近一次nextval@H_544_1502@返回的数值。这个函数等效于currval@H_544_1502@,只是它不用序列名为参数,它抓取当前会话里面最近一次nextval@H_544_1502@使用的序列。如果当前会话还没有调用过nextval@H_544_1502@,那么调用lastval将@H_544_1502@会报错。
setval(regclass,bigint) bigint 重置序列对象的计数器数值。设置序列的last_value@H_544_1502@字段为指定数值并且将其is_called@H_544_1502@字段设置为true@H_544_1502@,表示下一次nextval@H_544_1502@将在返回数值之前递增该序列。
setval(regclass,bigint,boolean) bigint 重置序列对象的计数器数值。功能等同于上面的setval函数,只是is_called@H_544_1502@可以设置为true@H_544_1502@或false@H_544_1502@。如果将其设置为false@H_544_1502@,那么下一次nextval@H_544_1502@将返回该数值,随后的nextval@H_544_1502@才开始递增该序列。

5.1查看下一个序列值

davID=# select nextval('tbl_xulIE_ID_seq'); nextval ---------       3(1 row)davID=# select nextval('tbl_xulIE_ID_seq'); nextval ---------       4(1 row)davID=# 

5.2 查看序列最近使用值

davID=# select nextval('tbl_xulIE_ID_seq'); nextval ---------       4(1 row)davID=# select currval('tbl_xulIE_ID_seq'); currval ---------       4(1 row)davID=# select currval('tbl_xulIE_ID_seq'); currval ---------       4(1 row)davID=# 

5.3 重置序列

方法一:使用序列函数

a.setval(regclass,bigint)

davID=# truncate tbl_xulIE;TruncATE tabledavID=# select setval('tbl_xulIE_ID_seq',1); setval --------      1(1 row)davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Sandy');                  INSERT 0 1davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'DavID');     INSERT 0 1davID=# select * from tbl_xulIE; ID | name  ----+-------  2 | Sandy  3 | DavID(2 rows)davID=# select currval('tbl_xulIE_ID_seq'); currval ---------       3(1 row)davID=# select nextval('tbl_xulIE_ID_seq'); nextval ---------       4(1 row)davID=# 

b.setval(regclass,boolean)

b.1setval(regclass,true)

1,true); setval -------- 1(1 row)davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'DavID');INSERT 0 1davID=# select * from tbl_xulIE; ID | name ----+------- 2 | Sandy 3 | DavID(2 rows)davID=#

效果同a.setval(regclass,bigint)

b.2setval(regclass,false)

-------- 1(1 row)davID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'DavID');INSERT 0 1davID=# select * from tbl_xulIE; ID | name ----+------- 1 | Sandy 2 | DavID(2 rows)davID=#

方法二:修改序列

修改序列的语法:

ALTER SEQUENCE name [ INCREMENT [ BY ] increment ]    [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]    [ START [ WITH ] start ]    [ RESTART [ [ WITH ] restart ] ]    [ CACHE cache ] [ [ NO ] CYCLE ]    [ OWNED BY { table.column | NONE } ]ALTER SEQUENCE name OWNER TO new_ownerALTER SEQUENCE name REname TO new_nameALTER SEQUENCE name SET SCHEMA new_schema

实例:

davID=# truncate tbl_xulIE;TruncATE tabledavID=# alter sequence tbl_xulIE_ID_seq restart with 0;ERROR:  RESTART value (0) cannot be less than MINVALUE (1)davID=# alter sequence tbl_xulIE_ID_seq restart with 1;ALTER SEQUENCEdavID=# insert into tbl_xulIE values (nextval('tbl_xulIE_ID_seq'),'Sandy');INSERT 0 1davID=# select * from tbl_xulIE; ID | name  ----+-------  1 | DavID  2 | Sandy(2 rows)davID=# select nextval('tbl_xulIE_ID_seq'); nextval ---------       3(1 row)davID=# 

六、删除序列

语法:

DROP SEQUENCE [ IF EXISTS ] name [,...] [ CASCADE | RESTRICT ]

当有表字段使用到PG序列时,不能直接删除。

davID=# drop sequence tbl_xulIE2_ID_seq;ERROR:  cannot drop sequence tbl_xulIE2_ID_seq because other objects depend on itDETAIL:  default for table tbl_xulIE2 column ID depends on sequence tbl_xulIE2_ID_seqHINT:  Use DROP ... CASCADE to drop the dependent objects too.davID=# drop table tbl_xulIE2;DROP tabledavID=# drop sequence tbl_xulIE2_ID_seq;DROP SEQUENCEdavID=# 

说明:对于序列是由建表时指定serial 创建的,删除该表的同时,对应的序列也会被删除。

七、参考资料

Postgresql官方说明:http://www.postgresql.org/docs/9.2/static/functions-sequence.html Postgresql: 数据迁移之序列问题:http://francs3.blog.163.com/blog/static/40576727201281351925766/ 总结

以上是内存溢出为你收集整理的PostgreSQL 序列(SEQUENCE)全部内容,希望文章能够帮你解决PostgreSQL 序列(SEQUENCE)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1174519.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)