Re: Adding Foreign Key constraints outside of table def.

From: "Gregory Wood" <gregw(at)com-stock(dot)com>
To: "Mike Haberman" <mikeh(at)ncsa(dot)uiuc(dot)edu>
Cc: "PostgreSQL-General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Adding Foreign Key constraints outside of table def.
Date: 2001-06-22 15:31:48
Message-ID: 001f01c0fb30$d6757dc0$7889ffcc@comstock.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> 1. Is there a way to add it without worrying about the
> syntax of "RI_FKey_..()" e.g. (this would be nice if it worked)
>
> ADD CONSTRAINT $fk.LocalColumnNames FOREIGN KEY
($fk.LocalColumnNames)
> REFERENCES $fk.ForeignTableName ($fk.ForeignColumnNames)
> ON DELETE set null, ON UPDATE cascade;

Here is how I added a foreign key to a table after the fact:

ALTER TABLE foo ADD FOREIGN KEY (bar) REFERENCES foo2 (bar2) ON DELETE SET
NULL;

Haven't actually tried within the ADD CONSTRAINST syntax.

> 2.... skipped

> 3. If I drop a table, how come the constainsts for that
> table are NOT dropped as well?

I don't know about this one, I hope someone more knowledgable will step in.
I'm surprised if this is indeed the behavior.

> 4. for circular references,
> Table A references B
> B references C
> C references A
>
> is it impossible to do an insert (assuming all the foreign key
> constraints are set up) ?

You'll have to set up at least two of the constraints as INITIALLY DEFERRED,
then perform any inserts to all three tables inside of a transaction.

Greg

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2001-06-22 15:52:00 Re: Re[4]: Postgres is too slow?
Previous Message Lamar Owen 2001-06-22 15:30:44 Re: Re: Authentication