Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 在Postgres中添加无效的外键_Postgresql_Foreign Keys_Ddl - Fatal编程技术网

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
;

请参阅,例如:

那么您自己发现的
无效
选项有什么问题?那么您自己发现的
无效
选项有什么问题?