改变序列以链接到另一个 table
Alter sequence to be linked to another table
所以我通过 Python 后端通过 HTTP 将大约 150,000 行数据上传到数据库中,上传需要一段时间,因此我将其插入到一个新的 table 中然后我交换(通过重命名)旧的 table:
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
table,以便当我删除 main
table 时,序列将保持链接到当前 tmp
table(新 main
)?
您可以将列 "owning" 设为序列
alter sequence main_id_seq
owned by main.id;
use 改变顺序:
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 | |
所以我通过 Python 后端通过 HTTP 将大约 150,000 行数据上传到数据库中,上传需要一段时间,因此我将其插入到一个新的 table 中然后我交换(通过重命名)旧的 table:
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
table,以便当我删除 main
table 时,序列将保持链接到当前 tmp
table(新 main
)?
您可以将列 "owning" 设为序列
alter sequence main_id_seq
owned by main.id;
use 改变顺序:
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 | |