highgo=# create sequence t_seq increment by 1 start with 1;
CREATE SEQUENCE
highgo=# select nextval('t_seq'); --查看序列中下一个值
nextval
---------
1
(1 行记录)
highgo=#create table t(id int default nextval(‘t_seq’),name varchar); --在定义时使用sequence
CREATE TABLE
highgo=# insert into t(name) values('jasmine'),('lily');
INSERT 0 2
highgo=# select * from t;
id | name
----+---------
2 | jasmine
3 | lily
(2 行记录)
highgo=# create table t1(id int,name varchar);
CREATE TABLE
highgo=# select nextval('t_seq');
nextval
---------
6
(1 行记录)
highgo=# insert into t1 values(nextval('t_seq'),'jim'); --在插入int数值时使用sequence
INSERT 0 1
highgo=# select * from t1;
id | name
----+------
7 | jim
(1 行记录)
PostgreSQL的sequence小例子
点赞
收藏