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 A34E7D1B189 for ; Wed, 26 May 2004 10:56:41 -0300 (ADT) 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 68697-07 for ; Wed, 26 May 2004 10:56:25 -0300 (ADT) Received: from smtp018.mail.yahoo.com (smtp018.mail.yahoo.com [216.136.174.115]) by svr1.postgresql.org (Postfix) with SMTP id 1C923D1B3CE for ; Wed, 26 May 2004 10:56:20 -0300 (ADT) Received: from unknown (HELO europa.janwieck.net) (janwieck@68.80.245.191 with login) by smtp018.mail.yahoo.com with SMTP; 26 May 2004 13:56:24 -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 i4QDuM018976; Wed, 26 May 2004 09:56:22 -0400 Message-ID: <40B4A201.1090405@Yahoo.com> Date: Wed, 26 May 2004 09:56:17 -0400 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: Bruno Wolff III Cc: Andreas , pgsql-sql@postgresql.org Subject: Re: Multitable uniqueness ? References: <40B40B4A.4050908@gmx.net> <20040526122013.GA472@wolff.to> In-Reply-To: <20040526122013.GA472@wolff.to> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=2.6 tagged_above=0.0 required=5.0 tests=RCVD_IN_DYNABLOCK, RCVD_IN_SORBS X-Spam-Level: ** X-Archive-Number: 200405/208 X-Sequence-Number: 17922 Bruno Wolff III wrote: > On Wed, May 26, 2004 at 05:13:14 +0200, > Andreas wrote: >> >> Is there a way to have something like this : UNIQUE (table_1.id, >> table_2.xxx) > > Postgres doesn't support database constraints at this time which is > what you would need to do this simply. > > You can enforce this constraint by creating a third table that has the > ids, id type and a separate id type field for each possible type that > will be null except for the type field that matches the actual type. > You also need to add a type field to the original two tables. Then you > set up a composite foreign key from the new table to each of the per type > tables using the id and the matching id type fields. Each per type table > should have a foriegn key refernce for id to the combined table. > This will enforce a 1-1 relationship between the combined table and the > union of the per type tables. I think a third table with just the unique id plus custom triggers on both tables that insert/update/delete the id into/from the third table are totally enough. Jan > > It is probably possible to get rid of the redundant copies of the field type > using triggers, but I don't know that there is much of a benefit to doing so. > The redundant values will all be kept in sync with constraints, so there isn't > a data consistancy problem with doing it that way. This method is going to be > more portable than using triggers. This method will probably be within a > constant factor as efficient as anything you do with triggers. > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly -- #======================================================================# # It's easier to get forgiveness for being wrong than for being right. # # Let's break this rule - forgive me. # #================================================== JanWieck@Yahoo.com #