云数据库 GaussDB-CREATE SEQUENCE:示例

时间:2023-11-01 16:22:10

示例

创建一个名为serial的递增序列,从101开始:

openGauss=# CREATE SEQUENCE serial START 101 CACHE 20;

从序列中选出下一个数字:

openGauss=# SELECT nextval('serial'); nextval  ---------      101

从序列中选出下一个数字:

openGauss=# SELECT nextval('serial'); nextval  ---------      102

创建与表关联的序列:

openGauss=# CREATE TABLE customer_address(    ca_address_sk             integer               not null,    ca_address_id             char(16)              not null,    ca_street_number          char(10)                      ,    ca_street_name            varchar(60)                   ,    ca_street_type            char(15)                      ,    ca_suite_number           char(10)                      ,    ca_city                   varchar(60)                   ,    ca_county                 varchar(30)                   ,    ca_state                  char(2)                       ,    ca_zip                    char(10)                      ,    ca_country                varchar(20)                   ,    ca_gmt_offset             decimal(5,2)                  ,    ca_location_type          char(20)                     );openGauss=# CREATE SEQUENCE serial1 START 101 CACHE 20OWNED BY customer_address.ca_address_sk;--删除表和序列openGauss=# DROP TABLE customer_address;openGauss=# DROP SEQUENCE serial cascade;openGauss=# DROP SEQUENCE serial1 cascade;
support.huaweicloud.com/centralized-devg-v2-opengauss/devg_03_0569.html