pg 序列


一、简介
一个序列对象通常用于为行或者表生成唯一的标识符。

二、创建序列
方法一:直接在表中指定字段类型为serial 类型
create table test (id serial not null primary key ,name varchar(10));
NOTICE:  CREATE TABLE will create implicit sequence "test_id_seq" for serial column "test.id"
CREATE TABLE

方法二:先创建序列,然后在新建的表中列属性指定序列,该列需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 } ]

先创建序列
create sequence test1_id_seq increment by 1 minvalue 1 no maxvalue start with 1;    
CREATE SEQUENCE

创建表指定该序列
create table test1(id int not null default nextval('test1_id_seq'), name varchar(10));
CREATE TABLE

三、查看序列
tina=# \d test1
                                Table "public.test1"
Column |         Type          |                     Modifiers                     
--------+-----------------------+----------------------------------------------------
id     | integer               | not null default nextval('test1_id_seq'::regclass)
name   | character varying(10) |

tina=# \d test1_id_seq
        Sequence "public.test1_id_seq"
    Column     |  Type   |        Value       
---------------+---------+---------------------
sequence_name | name    | test1_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 | f


四、序列应用
4.1 在INSERT 命令中使用序列
insert into test1 values (nextval('test1_id_seq'), 'David');     
INSERT 0 1
insert into test1 values (nextval('test1_id_seq'), 'tina');
INSERT 0 1
select * from test1;
id | name 
----+-------
  1 | David
  2 | tina
(2 rows)


4.2 数据有变后更新序列
指定序列从某个值从新开始计数
alter sequence test1_id_seq restart with 100;
ALTER SEQUENCE

查看当前序列值
select currval('test1_id_seq');
currval
---------
      2
(1 row)

查看下一个序列值
select nextval('test1_id_seq');
nextval
---------
     100
(1 row)

select nextval('test1_id_seq'); ---查一次增长一次
nextval
---------
     101
(1 row)

select nextval('test1_id_seq');
nextval
---------
     102
(1 row)

指定序列的值:
tina=# select setval('test1_id_seq', max(id)) from test1; --目前最大的id为2,将这个值赋给序列
setval
--------
      2
(1 row)

tina=# select nextval('test1_id_seq');
nextval
---------
       3
(1 row)


五、序列函数
函数 返回类型 描述
nextval(regclass) bigint 递增序列对象到它的下一个数值并且返回该值。这个动作是自动完成的。即使多个会话并发运行nextval,每个进程也会安全地收到一个唯一的序列值。

currval(regclass) bigint 在当前会话中返回最近一次nextval抓到的该序列的数值。此函数返回一个会话范围的数值,而且也能给出一个可预计的结果,因此可以用于判断其它会话是否执行过nextval。

lastval() bigint 返回当前会话里最近一次nextval返回的数值。这个函数等效于currval,只是它不用序列名为参数,它抓取当前会话里面最近一次nextval使用的序列。
如果当前会话还没有调用过nextval,那么调用lastval将会报错。

setval(regclass, bigint) bigint 重置序列对象的计数器数值。设置序列的last_value字段为指定数值并且将其is_called字段设置为true,表示下一次nextval将在返回数值之前递增该序列。

setval(regclass, bigint, boolean) bigint 重置序列对象的计数器数值。功能等同于上面的setval函数,只是is_called可以设置为true或false。如果将其设置为false,那么下一次nextval将返回该数值,随后的nextval才开始递增该序列。


select lastval();  --不需要指定序列名,只是当前会话最后一次nextval的值
lastval
---------
       3
(1 row)
其他几个上面已经使用过了。

六、修改序列
语法:
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 increment by 2 restart with 200;

修改属主和名称:  
alter sequence name owner to new_owner;
alter sequence name rename to new_name;
alter sequence name set schema new_schema;

授权:
grant select,update on sequence tbname_id_seq to username; 

七、删除序列
语法:
DROP SEQUENCE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
当有表字段使用到PG序列时,不能直接删除。

drop sequence test1_id_seq;
ERROR:  cannot drop sequence test1_id_seq because other objects depend on it
DETAIL:  default for table test1 column id depends on sequence test1_id_seq
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

drop table test1;
DROP TABLE

drop sequence test1_id_seq;
DROP SEQUENCE

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


猜你喜欢

转载自906179271.iteye.com/blog/2268971