PostgreSQL 序列(SEQUENCE)
发布时间:2020-12-13 18:19:34 所属栏目:百科 来源:网络整理
导读:一、简介 序列对象(也叫序列生成器)就是用CREATE SEQUENCE 创建的特殊的单行表。一个序列对象通常用于为行或者表生成唯一的标识符。 二、创建序列 方法一: 直接在表中指定字段类型为serial 类型 david = # create table tbl_xulie (david(# id serial,dav
一、简介 序列对象(也叫序列生成器)就是用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" 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 } ] 实例: create sequence tbl_xulie2_id_seq increment by 1 minvalue 1 no maxvalue start with 1;
CREATE SEQUENCE
david=#
tbl_xulie2 (
david(# id int4 not null default nextval('tbl_xulie2_id_seq'),0)">);
=#
三、查看序列 david=# d tbl_xulie Table "public.tbl_xulie" Column | Type | Modifiers --------+---------+-------------------------------------------------------- id | integer | tbl_xulie_id_seq::regclass) name text david# d tbl_xulie2 .tbl_xulie2" Modifiers ------+---------+--------------------------------------------------------- id =# 查看序列属性 # d tbl_xulie_id_seq
Sequence ".tbl_xulie_id_seq"
Column Value
-------------+---------+---------------------
sequence_name | name tbl_xulie_id_seq
last_value bigint
start_value
increment_by
max_value 9223372036854775807
min_value
cache_value
log_cnt 0
is_cycled | boolean f
is_called f
Owned by: .tbl_xulie.id
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 | | 9223372036854775807 | | 0 | f f
( row)
david=#
|