Re: Will a DELETE violate an FK?

From: "Albe Laurenz" <all(at)adv(dot)magwien(dot)gv(dot)at>
To: "Robert James *EXTERN*" <srobertjames(at)gmail(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Will a DELETE violate an FK?
Date: 2007-05-29 09:11:08
Message-ID: AFCCBB403D7E7A4581E48F20AF3E5DB203057722@EXADV1.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Is there anyway to know if a DELETE will violate an FK
> without actually trying it?

I don't know what you mean by 'without trying it', but does the
following answer your question?

CREATE TABLE a (id integer PRIMARY KEY);
CREATE TABLE b (id integer PRIMARY KEY,
a_id integer NOT NULL CONSTRAINT b_fkey REFERENCES a(id));

INSERT INTO a (id) VALUES (1);
INSERT INTO b (id, a_id) VALUES (42, 1);

DELETE FROM a WHERE id=1;
ERROR: update or delete on table "a" violates foreign key constraint
"b_fkey" on table "b"
DETAIL: Key (id)=(1) is still referenced from table "b".

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message danmcb 2007-05-29 09:20:37 Re: optimisation for a table with frequently used query
Previous Message Albe Laurenz 2007-05-29 09:02:13 Re: hundreds of schema vs hundreds of databases