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 84A2152C07 for ; Wed, 27 Jul 2005 17:37:08 -0300 (ADT) 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 73025-03 for ; Wed, 27 Jul 2005 20:37:03 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id 0330D52B4B for ; Wed, 27 Jul 2005 17:37:01 -0300 (ADT) Received: (qmail 4331 invoked by uid 500); 27 Jul 2005 20:37:59 -0000 Date: Wed, 27 Jul 2005 15:37:59 -0500 From: Bruno Wolff III To: Jeff Boes Cc: pgsql-sql@postgresql.org Subject: Re: Foreign key with check? Message-ID: <20050727203759.GA3884@wolff.to> Mail-Followup-To: Bruno Wolff III , Jeff Boes , pgsql-sql@postgresql.org References: <42E7E9B3.4080500@endpoint.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <42E7E9B3.4080500@endpoint.com> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200507/236 X-Sequence-Number: 22214 On Wed, Jul 27, 2005 at 16:08:19 -0400, Jeff Boes wrote: > Given a table like this: > > create table primary ( > a integer primary key, > b boolean > ); > > And another like this: > > create table secondary ( > a integer, > some_other_fields > ); > > > I would like a foreign key constraint on the "secondary" table that > looks something like: > > foreign key (a, true) references primary (a, b) > > That is, a row in "secondary" is allowed to reference a row in "primary" > if and only if that referenced row has (b = true). If what you are saying is that all entries in secondary need to reference a row in primary and in addition the referenced row must have b true, then you can do this fairly simply using a bit of extra space. Add a row b to secondary with a constraint that b = true. Then make the foreign key reference on both a and b.