一、简介序列对象(也叫序列生成器)就是用CREATESEQUENCE创建的特别的单行表。一个序列对象凡是用于为行或者表生成独一的标识符。二、创建序列办法一:直接在表中指定字段类型为serial类型david#createtabletbl_xulie(david(#idserial,david
一、简介
序列对象(也叫序列生成器)就是用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 TABLE
david=#
办法二:先创建序列名称,然后在新建的表中列属性指定序列就可以了,该列需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 minvalue no maxvalue start with ;
CREATE SEQUENCE
david=#
david=# create table tbl_xulie2 (
david(# id int4 not null default nextval(""tbl_xulie2_id_seq""),
david(# name text);
CREATE TABLE
david=#
三、查看序列
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 |
start_value | bigint |
increment_by | bigint |
max_value | bigint |
min_value | bigint |
cache_value | bigint |
log_cnt | bigint |
is_cycled | boolean | f
is_called | boolean | f
Owned by: public.tbl_xulie.id
david
=#
david=# * 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 | | | | | | | | f | f
( row)
david
=#
四、序列应用
4.1 在INSERT 号令中应用序列
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""David"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Sandy"");
INSERT
david=# * tbl_xulie;
id | name
----+-------
| David
| Sandy
( rows)
david
=#
4.2 数据迁徙后更新序列
david=# tbl_xulie;
TRUNCATE TABLE
david=#
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Sandy"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""David"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Eagle"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Miles"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Simon"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Rock"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Peter"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Sally"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Nicole"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Monica"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Renee"");
INSERT
david=# * tbl_xulie;
id | name
----+--------
| Sandy
| David
| Eagle
| Miles
| Simon
| Rock
| Peter
| Sally
| Nicole
| Monica
| Renee
( rows)
david
=# copy tbl_xulie to ""/tmp/tbl_xulie.sql"";
COPY
david=# tbl_xulie;
TRUNCATE TABLE
david=# alter sequence tbl_xulie_id_seq restart with ;
ALTER SEQUENCE
david=# currval(""tbl_xulie_id_seq"");
currval
---------
( row)
david
=# nextval(""tbl_xulie_id_seq"");
nextval
---------
( row)
david
=# nextval(""tbl_xulie_id_seq"");
nextval
---------
( row)
david
=# begin;
BEGIN
david=# copy tbl_xulie ""/tmp/tbl_xulie.sql"";
COPY
david=# setval(""tbl_xulie_id_seq"", max(id)) tbl_xulie;
setval
--------
( row)
david
=# end;
COMMIT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Flash"");
INSERT
david=# * tbl_xulie;
id | name
----+--------
| Sandy
| David
| Eagle
| Miles
| Simon
| Rock
| Peter
| Sally
| Nicole
| Monica
| Renee
| Flash
( rows)
david
=# nextval(""tbl_xulie_id_seq"");
nextval
---------
( row)
david
=#
五、序列函数
下面序列函数,为我们从序列对象中获取最新的序列值供给了简单和并发读取安然的办法。
函数
|
返回类型
|
描述
|
nextval(regclass)
|
bigint
|
递增序列对象到它的下一个数值并且返回该值。这个动作是主动完成的。即使多个会话并发运行nextval,每个过程也会安然地收到一个独一的序列值。
|
currval(regclass)
|
bigint
|
在当前会话中返回比来一次nextval抓到的该序列的数值。(若是在本会话中从未在该序列上调用过 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才开端递增该序列。
|
5.1 查看下一个序列值
david=# nextval(""tbl_xulie_id_seq"");
nextval
---------
( row)
david
=# nextval(""tbl_xulie_id_seq"");
nextval
---------
( row)
david
=#
5.2 查看序列比来应用值
david=# nextval(""tbl_xulie_id_seq"");
nextval
---------
( row)
david
=# currval(""tbl_xulie_id_seq"");
currval
---------
( row)
david
=# currval(""tbl_xulie_id_seq"");
currval
---------
( row)
david
=#
5.3 重置序列
办法一:应用序列函数
a. setval(regclass, bigint)
david=# tbl_xulie;
TRUNCATE TABLE
david=# setval(""tbl_xulie_id_seq"", );
setval
--------
( row)
david
=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Sandy"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""David"");
INSERT
david=# * tbl_xulie;
id | name
----+-------
| Sandy
| David
( rows)
david
=# currval(""tbl_xulie_id_seq"");
currval
---------
( row)
david
=# nextval(""tbl_xulie_id_seq"");
nextval
---------
( row)
david
=#
b. setval(regclass, bigint, boolean)
b.1 setval(regclass, bigint, true)
david=# tbl_xulie;
TRUNCATE TABLE
david=# setval(""tbl_xulie_id_seq"", , true);
setval
--------
( row)
david
=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Sandy"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""David"");
INSERT
david=# * tbl_xulie;
id | name
----+-------
| Sandy
| David
( rows)
david
=#
结果同a. setval(regclass, bigint)
b.2 setval(regclass, bigint, false)
david=# tbl_xulie;
TRUNCATE TABLE
david=# setval(""tbl_xulie_id_seq"", , false);
setval
--------
( row)
david
=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Sandy"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""David"");
INSERT
david=# * tbl_xulie;
id | name
----+-------
| Sandy
| David
( 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_owner
ALTER SEQUENCE name RENAME TO new_name
ALTER SEQUENCE name SET SCHEMA new_schema
实例:
david=# tbl_xulie;
TRUNCATE TABLE
david=# alter sequence tbl_xulie_id_seq restart with ;
ERROR: RESTART value () cannot be less than MINVALUE ()
david=# alter sequence tbl_xulie_id_seq restart with ;
ALTER SEQUENCE
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""David"");
INSERT
david=# into tbl_xulie values (nextval(""tbl_xulie_id_seq""), ""Sandy"");
INSERT
david=# * tbl_xulie;
id | name
----+-------
| David
| Sandy
( rows)
david
=# nextval(""tbl_xulie_id_seq"");
nextval
---------
( 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 it
DETAIL: default for table tbl_xulie2 column id depends on sequence tbl_xulie2_id_seq
HINT: Use DROP ... CASCADE to drop the dependent objects too.
david=# drop table tbl_xulie2;
DROP TABLE
david=# drop sequence tbl_xulie2_id_seq;
DROP SEQUENCE
david=#
申明:对于序列是由建表时指定serial
创建的,删除该表的同时,对应的序列也会被删除。