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;
知识分享,需人人参与,看完请点赞留言,共同讨论进步