Re: Foreign key creation

From: "Oliver Elphick" <olly(at)lfix(dot)co(dot)uk>
To: edp <edp(at)elgi(dot)jet(dot)co(dot)in>
Cc: "'pgsql-docs(at)postgresql(dot)org'" <pgsql-docs(at)postgresql(dot)org>
Subject: Re: Foreign key creation
Date: 2001-04-22 11:41:21
Message-ID: 200104221141.f3MBfLU16722@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

edp wrote:
>How to create foreign key in Posgresql.

There are two ways, as shown below:

CREATE TABLE supplier (
id INTEGER PRIMARY KEY,
name TEXT
);

CREATE TABLE invoice (
invno SERIAL PRIMARY KEY,
supplier INTEGER NOT NULL
REFERENCES supplier(id)
ON UPDATE CASCADE
ON DELETE RESTRICT
DEFERRABLE,
...etc...
);

CREATE TABLE location (
id CHAR(2) PRIMARY KEY
name TEXT NOT NULL
);

CREATE TABLE supplier_location (
supplier INTEGER NOT NULL
REFERENCES supplier(id)
ON UPDATE CASCADE
ON DELETE RESTRICT
DEFERRABLE,
location CHAR(2) NOT NULL
REFERENCES location(id)
ON UPDATE CASCADE
ON DELETE RESTRICT
DEFERRABLE
);

CREATE TABLE delivery (
product VARCHAR(10),
supplier INTEGER,
location CHAR(2),
...etc...,
PRIMARY KEY (product, supplier, location),
FOREIGN KEY (supplier, location) REFERENCES supplier_location
ON UPDATE CASCADE
ON DELETE RESTRICT
MATCH FULL
DEFERRABLE
);
--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47 6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"If my people, which are called by my name, shall
humble themselves, and pray, and seek my face, and
turn from their wicked ways; then will I hear from
heaven, and will forgive their sin, and will heal
their land." II Chronicles 7:14

Browse pgsql-docs by date

  From Date Subject
Next Message Yasuo Ohgaki 2001-04-22 13:52:39 V7.1 Manual (HTML integrated version)
Previous Message edp 2001-04-21 15:14:58 Foreign key creation