Received: from anchor-post-31.mail.demon.net (anchor-post-31.mail.demon.net [194.217.242.89]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f3MBfW312384 for ; Sun, 22 Apr 2001 07:41:32 -0400 (EDT) (envelope-from olly@lfix.co.uk) Received: from lfix.demon.co.uk ([158.152.59.127] helo=linda.lfix.co.uk) by anchor-post-31.mail.demon.net with esmtp (Exim 2.12 #1) id 14rIF1-0000uP-0V; Sun, 22 Apr 2001 12:41:23 +0100 Received: from lfix.co.uk (olly@localhost [127.0.0.1]) by localhost (8.11.3/8.11.3/Debian 8.11.2-1) with ESMTP id f3MBfLU16722; Sun, 22 Apr 2001 12:41:22 +0100 Message-Id: <200104221141.f3MBfLU16722@linda.lfix.co.uk> X-Mailer: exmh version 2.3.1 01/18/2001 (debian 2.3.1-1) with nmh-1.0.4+dev X-URL: http://www.lfix.co.uk/oliver X-face: "xUFVDj+ZJtL_IbURmI}!~xAyPC"Mrk=MkAm&tPQnNq(FWxv49R}\>0oI8VM?O2VY+N7@F- KMLl*!h}B)u@TW|B}6 cc: "'pgsql-docs@postgresql.org'" Subject: Re: Foreign key creation In-reply-to: Message from edp of Sat, 21 Apr 2001 20:44:58 +0530. <73010348951BD511B09E00A0C9ED2F0F08F747@ELGIMAIL> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Sun, 22 Apr 2001 12:41:21 +0100 From: "Oliver Elphick" X-Archive-Number: 200104/68 X-Sequence-Number: 640 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.Elphick@lfix.co.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