1

I have two tables which are connected by relations on few columns. I wanna create foreign key that will take care of cleaning connected entities but not by deleting them - I need to use "SET NULL" option.

My tables:

CREATE TABLE resource
(
  id integer NOT NULL,
  project_id integer NOT NULL,
  lang character(2) NOT NULL,
  recource_id integer,
  reserved_by integer NOT NULL,
  CONSTRAINT resource_v0_pkey PRIMARY KEY (id, project_id,  lang);

CREATE TABLE resource_list
(
  id integer NOT NULL
  project_id integer NOT NULL,
  lang character(2) NOT NULL,
  name character varying(128),
  CONSTRAINT resource_list_v0_pkey PRIMARY KEY (id, project_id,  lang);
  );

How can I add following FK to set recource_id to BULL when resource is deleted. It should look something like that:

ALTER TABLE resource
ADD CONSTRAINT $2
  FOREIGN KEY (resource_id, project_id, lang)
  REFERENCES resource_list(id, project_id, lang)
  ON DELETE SET NULL;

What is important I want set NULL only to resource.recource_id not for all connected fields. Is it possible?

4

1 回答 1

0

resource.id 是唯一的吗?如果是这样,请在此处添加一个唯一约束,以及设置为 null 的第二个外键。然后添加带有初始延迟检查的第二个外键。

于 2013-12-03T03:28:17.000 回答