当前位置:Gxlcms > 数据库问题 > PostgreSQL的sequence小例子

PostgreSQL的sequence小例子

时间:2021-07-01 10:21:17 帮助过:4人阅读


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小例子

标签:

人气教程排行