X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 76314D1D1CD for ; Tue, 17 Feb 2004 17:23:40 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 54100-06 for ; Tue, 17 Feb 2004 13:23:33 -0400 (AST) Received: from hotmail.com (sea2-dav46.sea2.hotmail.com [207.68.164.18]) by svr1.postgresql.org (Postfix) with ESMTP id 2B084D1CB1D for ; Tue, 17 Feb 2004 13:23:32 -0400 (AST) Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC; Tue, 17 Feb 2004 09:23:31 -0800 Received: from 67.22.47.125 by sea2-dav46.sea2.hotmail.com with DAV; Tue, 17 Feb 2004 17:23:31 +0000 X-Originating-IP: [67.22.47.125] X-Originating-Email: [gpatnude@hotmail.com] X-Sender: gpatnude@hotmail.com From: "Greg Patnude" To: "'Jan Wieck'" Cc: Subject: Re: Unique Constraint with foreign Key Date: Tue, 17 Feb 2004 09:22:44 -0800 Message-ID: <000301c3f57a$a7d95800$6401a8c0@dpanel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2627 In-Reply-To: <40322829.3050603@Yahoo.com> Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-OriginalArrivalTime: 17 Feb 2004 17:23:31.0788 (UTC) FILETIME=[C3E4E8C0:01C3F57A] X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/223 X-Sequence-Number: 16913 You've got it wrong when you reference the data column (a.x) -- your foreign key should reference the primary key in the referenced table (a.y)... Besides, in your table A -- 1, 99 2, 99 violates your unique constraint on column 'X' -- it would never happen... What I suggested is like this: create table a ( y integer not null primary key default nextval('nexta_seq'), x varchar not null UNIQUE ); create table b ( z integer not null PRIMARY KEY default nextval('nextbz_seq'), x integer NOT NULL REFERENCES a(y), ); Table A would have 1, 99 2, 99 .. 99,99 and table B would have 1, 1 1, 2 .. 1, 99 Greg Patnude / The Digital Demention 2916 East Upper Hayden Lake Road Hayden Lake, ID. 83835 (208) 762-0762 Send replies to: gpatnude@adelphia.net Website: http://www.left-center.com -----Original Message----- From: Jan Wieck [mailto:JanWieck@Yahoo.com] Sent: Tuesday, February 17, 2004 6:42 AM To: Greg Patnude Cc: pgsql-sql@postgresql.org Subject: Re: [SQL] Unique Constraint with foreign Key Greg Patnude wrote: > Pleas also note that the referenced column in the foreign table either needs > to be the PRIMARY KEY or have a unique constraint on it or maybe it just > requires an index on it -- I'm not sure but I discovered that if the column > in the foreign table (containing the REFERENCED key...) is NOT the primary > key column -- the REFERENCES a(x) will faill unless a.x is specified as > 'UNIQUE' -- as in the following example: This is according to the SQL specification, which doesn't like doubts. Imagine rows (1, 99), (2, 99) in table a and row (3, 99) in table b. Which of the a-rows is now referenced and am I allowed to delete the other? There are good arguments either way, but if you require a UNIQUE on a.x, then this question will never come up. Jan > > create table a ( > y integer not null primary key default nextval('nexta_seq'), > x varchar not null UNIQUE > > ); > > create table b ( > > z integer not null PRIMARY KEY default nextval('nextbz_seq'), > x varchar NOT NULL REFERENCES a(x), > > ); > > -- #======================================================================# # It's easier to get forgiveness for being wrong than for being right. # # Let's break this rule - forgive me. # #================================================== JanWieck@Yahoo.com #