数据仓库服务 GAUSSDB(DWS)-数据库SEQUENCE优秀实践:创建sequence的两种方式

时间:2024-09-14 17:38:49

创建sequence的两种方式

方式一:使用CREATE SEQUENCE语句创建序列,在新建的表中通过nextval调用。

1
2
3
4
5
CREATE SEQUENCE seq_test increment by 1 minvalue 1 no maxvalue start with 1;
CREATE SEQUENCE

CREATE TABLE table_1(id int not null default nextval('seq_test'), name text);
CREATE TABLE

方式二:建表时使用serial类型,会自动创建一个sequence,并且会将该列的默认值设置为nextval。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
CREATE TABLE mytable(a int, b serial) distribute by hash(a);
NOTICE:  CREATE TABLE will create implicit sequence "mytable_b_seq" for serial column "mytable.b"
CREATE TABLE

 \d+ mytable
                                            Table "dbadmin.mytable"
 Column |  Type   |                      Modifiers                      | Storage | Stats target | Description
--------+---------+-----------------------------------------------------+---------+--------------+-------------
 a      | integer |                                                     | plain   |              |
 b      | integer | not null default nextval('mytable_b_seq'::regclass) | plain   |              |
Has OIDs: no
Distribute By: HASH(a)
Location Nodes: ALL DATANODES
Options: orientation=row, compression=no
support.huaweicloud.com/bestpractice-dws/dws_05_0113.html