Sql 更改要链接到另一个表的序列
因此,我通过Python后端通过HTTP将大约150000行数据上传到数据库中,上传需要一段时间,因此我将其插入一个新表中,然后(通过重命名)与旧表交换:Sql 更改要链接到另一个表的序列,sql,postgresql,sequence,alter,postgresql-9.5,Sql,Postgresql,Sequence,Alter,Postgresql 9.5,因此,我通过Python后端通过HTTP将大约150000行数据上传到数据库中,上传需要一段时间,因此我将其插入一个新表中,然后(通过重命名)与旧表交换: create table tmp (like main); alter sequence main_id_seq restart; alter table tmp alter column id set default nextval('main_id_seq'); drop table main cascade; -- THIS REMO
create table tmp (like main);
alter sequence main_id_seq restart;
alter table tmp alter column id set default nextval('main_id_seq');
drop table main cascade; -- THIS REMOVES THE SEQUENCE ^^^^^^^
alter table tmp rename to main;
如何将序列更改为不链接到
main
表,以便在我删除main
表时,序列将保持链接到当前tmp
表(新建main
)?您可以通过使列“拥有”序列来实现这一点
alter sequence main_id_seq
owned by main.id;
改变顺序:
t=# create table s120(i bigserial);
CREATE TABLE
t=# \d+ s120;
Table "public.s120"
Column | Type | Modifiers | Storage | Stats target | Description
--------+--------+--------------------------------------------------+---------+--------------+-------------
i | bigint | not null default nextval('s120_i_seq'::regclass) | plain | |
t=# create table s121(i bigint);
CREATE TABLE
t=# alter sequence s120_i_seq owned by s121.i;
ALTER SEQUENCE
t=# drop table s120;
DROP TABLE
t=# alter table s121 alter COLUMN i set default nextval('s120_i_seq'::regclass);
ALTER TABLE
t=# \d+ s121
Table "public.s121"
Column | Type | Modifiers | Storage | Stats target | Description
--------+--------+-----------------------------------------+---------+--------------+-------------
i | bigint | default nextval('s120_i_seq'::regclass) | plain | |
我想一定有这样一个简单的方法,但是在任何地方都找不到属于的
。。。谢谢@马库斯梅斯卡宁:就在这里:是的,我需要更多地阅读文档,我对PSQL还是相当陌生的。下次会做进一步的研究,谢谢:)