X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 3A0C3DB092 for ; Tue, 22 Nov 2005 11:40:20 -0400 (AST) 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 65408-03 for ; Tue, 22 Nov 2005 15:40:23 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from jandor.look.ca (theta1.look.ca [207.136.80.122]) by svr1.postgresql.org (Postfix) with ESMTP id CC570DB081 for ; Tue, 22 Nov 2005 11:40:16 -0400 (AST) Received: from 216-154-14-116.dsl.look.ca ([216.154.14.116]) by jandor.look.ca with esmtp (Exim 4.20) id 1EeaFo-0006ZK-Kp; Tue, 22 Nov 2005 15:40:20 +0000 From: Rod Taylor To: Joost Kraaijeveld Cc: "Pgsql-sql@postgresql.org" In-Reply-To: <1132673053.5701.9.camel@Panoramix> References: <1132673053.5701.9.camel@Panoramix> Date: Tue, 22 Nov 2005 10:39:56 -0500 Message-Id: <1132673996.970.223.camel@home> Mime-Version: 1.0 X-Mailer: Evolution 2.4.1 FreeBSD GNOME Team Port X-SA-Exim-Mail-From: pg@rbt.ca Subject: Re: Foreign key to 2 tables problem Content-Type: text/plain Content-Transfer-Encoding: 7bit X-SA-Exim-Version: 3.1 (built Tue Feb 24 05:09:27 GMT 2004) X-SA-Exim-Scanned: Yes X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200511/198 X-Sequence-Number: 23449 On Tue, 2005-11-22 at 16:24 +0100, Joost Kraaijeveld wrote: > Hi, > > Is there a way to create a foreign key to 2 tables: e.g. a bankaccount > table that has a column "owner", that must point to a record in either > the customer or the supplier table? No. What you need is an owner table that customers and suppliers are inherited from. Put your 'entity' data into the owner table. Put customer and supplier specific information into the customer and supplier structures. create table owner (owner_name varchar(120) primary key, ...); create table customer (customer_name varchar(120) references owner, ...); create table supplier (supplier_name varchar(120) references owner, ...); create table bankaccount (owner_name varchar(120) references owner); You can use a periodic check to ensure that all owners are a customer or supplier just incase your code breaks. Incidentally, this also allows a single entity with a single bankaccount to be both a customer and a supplier. They can supply you with product X and purchase product Y without 2 different accounts. --