示例架构:http://sqlfiddle.com/#!1/3d410
我已经有了一个表,我想向该表添加一个新的、无效的外键。添加 NOT VALID 外键的正确语法是什么?
CREATE TABLE junks (
id serial PRIMARY KEY,
name text
);
CREATE TABLE trunks (
id serial PRIMARY KEY,
name text
-- no fk
);
-- and the below does not work!
--ALTER TABLE trunks ADD junk serial REFERENCES junks(id) NOT VALID;
最佳答案
您首先添加列:
alter table trunks add column junk serial;
然后将约束添加到表中:
alter table trunks add constraint the_constraint_name
FOREIGN KEY (junk)
REFERENCES junks (id)
not valid;
关于postgresql - 在 Postgres 中添加 NOT VALID 外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13972559/