X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 629BD539B9 for ; Sun, 27 Mar 2005 08:47:36 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43115-09 for ; Sun, 27 Mar 2005 07:47:26 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 54BE953347 for ; Sun, 27 Mar 2005 08:47:25 +0100 (BST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j2R7lLqw038160 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 27 Mar 2005 00:47:23 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j2R7lLqF046334; Sun, 27 Mar 2005 00:47:21 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j2R7lKOC046333; Sun, 27 Mar 2005 00:47:20 -0700 (MST) (envelope-from mfuhr) Date: Sun, 27 Mar 2005 00:47:20 -0700 From: Michael Fuhr To: ASAKALAL@bouyguestelecom.fr Cc: pgsql-sql@postgresql.org Subject: Re: Foreign key Message-ID: <20050327074720.GB46207@winnie.fuhr.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/350 X-Sequence-Number: 20829 On Fri, Mar 25, 2005 at 04:31:16PM +0100, ASAKALAL@bouyguestelecom.fr wrote: > > When i add table with foreign key in my database, this error return : < > number of referencing and referenced colums for foreign key disagree>. Apparently the referencing key (the foreign key specification) has a different number of columns than the referenced key (the primary key or other unique key in the referenced table). Here's an example that illustrates the problem: CREATE TABLE foo ( pk1 integer NOT NULL, pk2 integer NOT NULL, PRIMARY KEY (pk1, pk2) -- 2-column primary key ); CREATE TABLE bar ( fk integer NOT NULL REFERENCES foo -- 1-column foreign key ); ERROR: number of referencing and referenced columns for foreign key disagree In the above example we need a 2-column foreign key: CREATE TABLE bar ( fk1 integer NOT NULL, fk2 integer NOT NULL, FOREIGN KEY (fk1, fk2) REFERENCES foo ); Here's another example that references a 1-column unique key that isn't a primary key: CREATE TABLE foo ( pk1 integer NOT NULL, pk2 integer NOT NULL, x integer NOT NULL, PRIMARY KEY (pk1, pk2), UNIQUE (x) ); CREATE TABLE bar ( fk integer NOT NULL REFERENCES foo (x) ); If these examples don't help, then please post the table definitions you're working with and explain what you'd like to do. -- Michael Fuhr http://www.fuhr.org/~mfuhr/