Re: [SQL] two tables - foreign keys referring to each other...

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Chris Czeyka <czeyka(at)skwea(dot)co(dot)jp>
Cc: pgsql-sql(at)postgresql(dot)org, pgsql-general(at)postgresql(dot)org
Subject: Re: [SQL] two tables - foreign keys referring to each other...
Date: 2001-02-21 07:34:30
Message-ID: Pine.BSF.4.21.0102202333230.6437-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql


You have to use ALTER TABLE to add the constraint to one of the tables.
Deferred refers to the checking of the constraint itself, not really
to the check to see if the table is there.

On Wed, 21 Feb 2001, Chris Czeyka wrote:

> Hey to all,
>
> I got two tables, linked to each other. How can I tell the first CREATE TABLE
> (institute_t) to wait to check the foreign key for the second table??? just
> like "hold on a little bit... you'll receive your admin_t" :-) ? I thoght
> DEFERRABLE, DEFERRED and transaction with BEGIN/COMMIT take care of this.
>
> ..or generally: how do you create two crosslinked foreign keyed tables?
>
> hopefully an easy problem for the real professionals!
>
>
> -----------------> here we go
> BEGIN; -- begin table transaction -- Only Postgresql
> CREATE TABLE institute_t (
> name VARCHAR(48) PRIMARY KEY,
> street VARCHAR(48) NOT NULL,
> zip VARCHAR(16),
> town VARCHAR(32) NOT NULL,
> country CHAR(2) NOT NULL, /* country codes ISO-3166*/
> phone VARCHAR(32) NOT NULL,
> fax VARCHAR(32),
> admin VARCHAR(16) REFERENCES admin_t
> ON UPDATE CASCADE
> ON DELETE SET NULL
> DEFERRABLE
> INITIALLY DEFERRED
> );
>
> CREATE TABLE admin_t (
> login VARCHAR(16) PRIMARY KEY,
> password VARCHAR(16) NOT NULL,
> email VARCHAR(32) NOT NULL,
> real_name VARCHAR(32) NOT NULL,
> street VARCHAR(48) NOT NULL,
> zip VARCHAR(16),
> town VARCHAR(32) NOT NULL,
> country CHAR(2) NOT NULL, /* country codes -- refer to
> ISO-3166*/
> phone VARCHAR(32) NOT NULL,
> fax VARCHAR(32),
> access INTEGER NOT NULL,
> institute VARCHAR(48) REFERENCES institute_t
> ON UPDATE CASCADE
> ON DELETE SET NULL
> DEFERRABLE
> INITIALLY DEFERRED
> );
> COMMIT;
>
>
> of course I get the ERROR, that admin_t doesn't exist. So? help the stupid!
> pls!
>
> best greets,
> Chris
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2001-02-21 07:44:15 Re: Weird indices
Previous Message Steff 2001-02-21 07:30:40 problems with copy

Browse pgsql-sql by date

  From Date Subject
Next Message Frank Joerdens 2001-02-21 08:38:15 Re: two tables - foreign keys referring to each other...
Previous Message Ken Kline 2001-02-21 05:14:02 logging a script