标签:PostgreSQL seq nextval 序列 id select row
PostgreSQL中的序列可以当作MySQL中的auto_increment来使用,但是序列并不是仅仅用于自增列。也就是说:
PostgreSQL SERIAL != MySQL SERIAL
第一,PostgreSQL提供了一个serial数据类型。有smallserial、serial、bigserial,分别占用了2、4、8个字节的内存。相应的最大值为32767、2147483674、9223372036854775807。
MySQL中有个关键字 serial,是bigint not null auto_increment unique。
自增示例
作为示例,来创建个表:
b=# create table abce(x serial,y char(20), z char(20)); CREATE TABLE b=# \d abce Table "public.abce" Column | Type | Collation | Nullable | Default --------+---------------+-----------+----------+--------------------------------- x | integer | | not null | nextval('abce_x_seq'::regclass) y | character(20) | | | z | character(20) | | | b=#
列x从序列abce_x_seq通过使用nextval()自动获取默认值。
继续插入一些数据:
b=# insert into abce(y,z) values(100,200),(300,450); INSERT 0 2 b=# select * from abce; x | y | z ---+----------------------+---------------------- 1 | 100 | 200 2 | 300 | 450 (2 rows) b=#
我们并没有显式插入x的值。
使用\d看看表和序列:
b=# \d List of relations Schema | Name | Type | Owner --------+------------+----------+---------- public | abce | table | postgres public | abce_x_seq | sequence | postgres (2 rows) b=#
序列本身
序列本身并不需要依赖表的声明定义。在下面的例子中,会创建一个从1001开始的序列。使用函数nextval()获取序列的下一个值:
b=# create sequence order_id start 1001; CREATE SEQUENCE b=# select nextval('order_id'); nextval --------- 1001 (1 row) b=# select nextval('order_id'); nextval --------- 1002 (1 row) b=#
有两种方式来检查当前的值,第一种是直接查询序列:
b=# select * from order_id; last_value | log_cnt | is_called ------------+---------+----------- 1002 | 31 | t (1 row) b=#
第二种,使用函数currval()
b=# select currval('order_id'); currval --------- 1002 (1 row) b=#
序列不是series
一个series,顾名思义是一系列数字。但是它们不能像序列一样被引用到列的自动评估中。但是它们对于生成数据非常方便。
b=# create table test1 as (select generate_series(1,100) as id); SELECT 100 b=# \d test1 Table "public.test1" Column | Type | Collation | Nullable | Default --------+---------+-----------+----------+--------- id | integer | | | b=# select * from test1 limit 5; id ---- 1 2 3 4 5 (5 rows) b=#
序列的wraparoud
序列可以在你想要的数字开始或结束,如果使用CYCLE限定符,也可以环绕以重新开始。在这里,我们创建了一个或两个环绕的重复序列。
b=# create sequence wrap_seq as int minvalue 1 maxvalue 2 CYCLE; CREATE SEQUENCE b=# select NEXTVAL('wrap_seq'); nextval --------- 1 (1 row) b=# select NEXTVAL('wrap_seq'); nextval --------- 2 (1 row) b=# select NEXTVAL('wrap_seq'); nextval --------- 1 (1 row) b=# select NEXTVAL('wrap_seq'); nextval --------- 2 (1 row) b=# select NEXTVAL('wrap_seq'); nextval --------- 1 (1 row) b=#
其它细节
可以通过\d查看序列的状态:
b=# \d order_id; Sequence "public.order_id" Type | Start | Minimum | Maximum | Increment | Cycles? | Cache --------+-------+---------+---------------------+-----------+---------+------- bigint | 1001 | 1 | 9223372036854775807 | 1 | no | 1 b=# \d wrap_seq; Sequence "public.wrap_seq" Type | Start | Minimum | Maximum | Increment | Cycles? | Cache ---------+-------+---------+---------+-----------+---------+------- integer | 1 | 1 | 2 | 1 | yes | 1 b=#
标签:PostgreSQL,seq,nextval,序列,id,select,row 来源: https://www.cnblogs.com/abclife/p/16437453.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。