Re: referential integrity on existing table

From: "Paul Laub" <plaub(at)incyte(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: referential integrity on existing table
Date: 2001-12-11 00:38:37
Message-ID: 006b01c181dc$2c49b220$ec02520a@incyte.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Hi Everyone
>
> My question is, is it possible to create a referential integrity constraint
> on an exisiting, already populated, table??
> My reasons for this are:

How about

ALTER TABLE tablename ADD CONSTRAINT constraint_name FOREIGN KEY
(childfield) REFERENCES parenttable (parentfield);

One downside: If anything goes wrong (some record violates referential
integrity), the command will fail without stating what record caused
the failure. So you might try something like the following first

SELECT * FROM childtable WHERE NOT EXISTS
(SELECT * FROM parenttable WHERE
childtable.keyfield = parenttable.keyfield);

to locate potentially offending records.

Paul

>
> Adam Fisher

Paul B. Laub http://astatine.incyte.com/laub (650) 845-5411 (voice)
Incyte Genomics, Inc. 3160 Porter Dr. Palo Alto, CA 94304 plaub(at)incyte(dot)com
*** Incite genomics! ***

Attachment Content-Type Size
Paul B Laub.vcf text/x-vcard 205 bytes

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2001-12-11 00:39:41 Re: Suitability of postgres for very high transaction volume
Previous Message Tom Lane 2001-12-11 00:32:43 Re: Vacuum errors and warnings