代码改变世界

【PostgreSQL】PostgreSQL中的序列

  abce  阅读(1647)  评论(0编辑  收藏  举报

PostgreSQL中的序列可以当作MySQL中的auto_increment来使用,但是序列并不是仅仅用于自增列。也就是说:

1
PostgreSQL SERIAL != MySQL SERIAL

  

第一,PostgreSQL提供了一个serial数据类型。有smallserial、serial、bigserial,分别占用了2、4、8个字节的内存。相应的最大值为32767、2147483674、9223372036854775807。

MySQL中有个关键字 serial,是bigint not null auto_increment unique。

自增示例

作为示例,来创建个表:

1
2
3
4
5
6
7
8
9
10
11
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()自动获取默认值。

继续插入一些数据:

1
2
3
4
5
6
7
8
9
10
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看看表和序列:

1
2
3
4
5
6
7
8
9
b=# \d
             List of relations
 Schema |    Name    |   Type   |  Owner  
--------+------------+----------+----------
 public | abce       | table    | postgres
 public | abce_x_seq | sequence | postgres
(2 rows)
 
b=#

  



序列本身

序列本身并不需要依赖表的声明定义。在下面的例子中,会创建一个从1001开始的序列。使用函数nextval()获取序列的下一个值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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=#

  

 

有两种方式来检查当前的值,第一种是直接查询序列:

1
2
3
4
5
6
7
b=# select * from order_id;
 last_value | log_cnt | is_called
------------+---------+-----------
       1002 |      31 | t
(1 row)
 
b=#

  

 

第二种,使用函数currval()

1
2
3
4
5
6
7
b=# select currval('order_id');
 currval
---------
    1002
(1 row)
 
b=#

  

 

序列不是series

一个series,顾名思义是一系列数字。但是它们不能像序列一样被引用到列的自动评估中。但是它们对于生成数据非常方便。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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限定符,也可以环绕以重新开始。在这里,我们创建了一个或两个环绕的重复序列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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查看序列的状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
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=#

  





相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-07-05 Python运算符优先级
点击右上角即可分享
微信分享提示