Re: two tables - foreign keys referring to each other... - Mailing list pgsql-sql

From Grigoriy G. Vovk
Subject Re: two tables - foreign keys referring to each other...
Date
Msg-id Pine.LNX.4.31.0102211056420.10373-100000@vovk.internal.linustech.com.cy
Whole thread Raw
In response to two tables - foreign keys referring to each other...  (Chris Czeyka <czeyka@skwea.co.jp>)
List 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



pgsql-sql by date:

Previous
From: Frank Joerdens
Date:
Subject: Re: two tables - foreign keys referring to each other...
Next
From: Josh Berkus
Date:
Subject: two tables - foreign keys referring to each other...]