X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 0DBB311F621B for ; Sat, 29 Apr 2006 19:04:17 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 85594-07-2 for ; Sat, 29 Apr 2006 19:03:51 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 5FB7911F6253 for ; Sat, 29 Apr 2006 19:02:46 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k3TM2fGJ009555; Sat, 29 Apr 2006 18:02:41 -0400 (EDT) To: Bruno Wolff III cc: Jeff Frost , Phillip Tornroth , pgsql-sql@postgresql.org Subject: Re: Multi-Column Constraints and Null Values In-reply-to: <20060429202737.GB14915@wolff.to> References: <303D0B49-F8EC-491C-8865-B7B819120C13@intellidot.net> <20060429202737.GB14915@wolff.to> Comments: In-reply-to Bruno Wolff III message dated "Sat, 29 Apr 2006 15:27:37 -0500" Date: Sat, 29 Apr 2006 18:02:41 -0400 Message-ID: <9554.1146348161@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200604/219 X-Sequence-Number: 25028 Bruno Wolff III writes: > To block multiple entries where one of the columns is null he needs to > add extra checks. One way to do this is to add a partial index for each column > with the condition that the other column IS NULL. This will cover everything > but the case where both columns are null. If this case isn't allowed, then > a row constraint can be used to block it. If it is allowed, the only thing that > comes to mind is making an SQL function that counts the number of rows where > both columns are null and calling that function in a row constraint and > checking that the result is <= 1. Nah, it's easy: CREATE UNIQUE INDEX fooi ON foo ((0)) WHERE col1 IS NULL AND col2 IS NULL; So you'd need a total of four unique indexes (3 of 'em partial) to enforce the condition for two columns. The real problem with this is that it doesn't scale well for more than two columns :-( regards, tom lane