postgresql序列基本操作

1、创建序列

CREATE SEQUENCE if not exists test_mergetable_id_seq

INCREMENT 1
MINVALUE 1
MAXVALUE 999999999
START 1
CACHE 1;
//或者:
create sequence if not exists test_mergetable_id_seq increment by 1 minvalue 1 no maxvalue start with 1;
2、指定序列(给表的主键指定创建好的序列)
alter table test_mergetable alter column "i_id" set default nextval('test_mergetable_id_seq');
3、设置序列自增长从当前最大值开始

SELECT setval('test_mergetable_id_seq', (SELECT MAX(i_id) FROM test_mergetable));

alter sequence test_mergetable_id_seq start with 12;

4、删除序列

drop sequence IF EXISTS test_mergetable_id_seq

5、查看序列

select nextval(seq_ commodity);返回下一个序列号后

select currval(seq_ commodity);返回当前的序列号

6、序列号改成和当前最大主键一致

select setval('xxx_id_seq', max(id)) from tableName;

posted @ 2022-12-05 14:21  数据库集中营  阅读(1507)  评论(0编辑  收藏  举报