Postgresql 在Postgres中添加无效的外键
模式示例: 我已经有一个表,我想向表中添加一个新的无效外键。添加无效外键的正确语法是什么Postgresql 在Postgres中添加无效的外键,postgresql,foreign-keys,ddl,Postgresql,Foreign Keys,Ddl,模式示例: 我已经有一个表,我想向表中添加一个新的无效外键。添加无效外键的正确语法是什么 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
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;
首先添加列:
alter table trunks add column junk serial;
然后将约束添加到表中:
alter table trunks add constraint the_constraint_name
FOREIGN KEY (junk)
REFERENCES junks (id)
not valid;
这项工作:
ALTER TABLE trunks ADD CONSTRAINT FK_junk_id
FOREIGN KEY (id)
REFERENCES junks(id)
NOT VALID
;
请参见,例如:此功能:
ALTER TABLE trunks ADD CONSTRAINT FK_junk_id
FOREIGN KEY (id)
REFERENCES junks(id)
NOT VALID
;
请参阅,例如:那么您自己发现的
无效选项有什么问题?那么您自己发现的无效选项有什么问题?