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 502ACD1DAB1 for ; Tue, 17 Feb 2004 15:46:45 +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 05986-10 for ; Tue, 17 Feb 2004 11:46:46 -0400 (AST) Received: from smtp018.mail.yahoo.com (smtp018.mail.yahoo.com [216.136.174.115]) by svr1.postgresql.org (Postfix) with SMTP id CE047D1D25D for ; Tue, 17 Feb 2004 11:46:41 -0400 (AST) Received: from unknown (HELO europa.janwieck.net) (janwieck@68.80.245.191 with login) by smtp018.mail.yahoo.com with SMTP; 17 Feb 2004 15:46:45 -0000 Received: from Yahoo.com (ismtp.afilias.com [216.217.55.254]) (authenticated) by europa.janwieck.net (8.11.6/8.11.6) with ESMTP id i1HFkhj05219; Tue, 17 Feb 2004 10:46:43 -0500 Message-ID: <40322829.3050603@Yahoo.com> Date: Tue, 17 Feb 2004 09:41:45 -0500 From: Jan Wieck User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Greg Patnude Cc: pgsql-sql@postgresql.org Subject: Re: Unique Constraint with foreign Key References: <64366.138.88.163.115.1076432616.squirrel@physics.gmu.edu> <20040210201521.GA13737@wolff.to> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/208 X-Sequence-Number: 16898 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 #