腾讯云数据仓库 TCHouse-P 自增列与序列的用法

[tbase@VM_0_29_centos tbase_mgr]$ psql -p 15432 -U tbase -d postgrespsql (PostgreSQL 10 (TBase 2.01))Type “help” for help.– 建立序列postgres=# create sequence tbase_seq;CREATE SEQUENCE– 建立序列,不存在时才创建postgres=# c。

相关产品
腾讯云数据仓库 TCHouse-P 产品页,含版本规格、价格与购买入口。

一、序列在 DML 中使用

— 插入数据,使用序列postgres=# INSERT INTO t (id, nickname) VALUES (nextval('tbase_seq'), 'TBase 好');INSERT 0 1postgres=# select * from t; id | nickname—-+———– 1 | 腾讯 TBase 2 | TBase 好(2 rows)

二、序列作为字段的默认值使用

— 将序列设置为字段的默认值postgres=# alter table t alter column id set default nextval('tbase_seq');– 插入数据,字段使用序列默认值postgres=# INSERT INTO t (nickname) VALUES ('hello TBase');postgres=# select * from t; id | nickname—-+————- 3 | hello TBase 1 | 腾讯 TBase 2 | TBase 好(3 rows)

三、序列作为字段类型使用

— 删除表postgres=# drop table t;DROP TABLE– 创建表,字段使用序列类型postgres=# create table t (id serial not null, nickname text);CREATE TABLE– 插入数据,字段自动使用序列postgres=# INSERT INTO t (nickname) VALUES ('hello TBase');INSERT 0 1postgres=# select * from t; id | nickname—-+————- 1 | hello TBase(1 row)

四、删除序列

— 删除序列postgres=# drop sequence tbase_seq;DROP SEQUENCE– 删除序列,不存在时跳过postgres=# drop sequence IF EXISTS tbase_seq;NOTICE: sequence "tbase_seq" does not exist, skippingDROP SEQUENCE

赞 (0)
上一篇 4天前
下一篇 4天前