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

From: "Grigoriy G(dot) Vovk" <Grigoriy(dot)Vovk(at)linustech(dot)com(dot)cy>
To: Chris Czeyka <czeyka(at)skwea(dot)co(dot)jp>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: two tables - foreign keys referring to each other...
Date: 2001-02-21 09:02:44
Message-ID: Pine.LNX.4.31.0102211056420.10373-100000@vovk.internal.linustech.com.cy
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

I think, if it is relationship many-to-many (one admin can be in many
institute, and one institute can has many admin, you should use relation
table, see below.

> -----------------> 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 institute_admin (
row int primary key,
name varchar(48) references institute_t,
login varchar(16) references admin_t
);

> 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;
>

If you have diffarant relation, describe it.

-----------------------------
Grigoriy G. Vovk

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Renaud Tthonnart 2001-02-21 09:15:38 sequence and stored procedure
Previous Message Pete Forman 2001-02-21 08:53:58 Re: vacuum analyze again...

Browse pgsql-sql by date

  From Date Subject
Next Message Derek 2001-02-21 15:45:03 changing column data types
Previous Message Frank Joerdens 2001-02-21 08:38:15 Re: two tables - foreign keys referring to each other...