一、序列在 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