X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 3E00F4764B4 for ; Tue, 6 May 2003 12:01:28 -0400 (EDT) Received: from randalls-laptop.kicks-ass.net (h00062577286a.ne.client2.attbi.com [24.61.43.102]) by postgresql.org (Postfix) with ESMTP id 59087474E42 for ; Tue, 6 May 2003 12:01:16 -0400 (EDT) Received: from tercent.net (localhost [127.0.0.1]) by randalls-laptop.kicks-ass.net (8.12.9/8.12.6) with ESMTP id h46G1Kfd005296 for ; Tue, 6 May 2003 12:01:21 -0400 (EDT) Date: Tue, 6 May 2003 12:01:20 -0400 Mime-Version: 1.0 (Apple Message framework v552) Content-Type: text/plain; charset=US-ASCII; format=flowed Subject: UNIQUE boolean: only one row may be "true" From: Randall Lucas To: pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit Message-Id: X-Mailer: Apple Mail (2.552) X-Spam-Status: No, hits=-8.9 required=5.0 tests=BAYES_10,USER_AGENT_APPLEMAIL autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200305/91 X-Sequence-Number: 13256 Hi folks, After a late night of SQL hacking, my brain fizzed out: How shall I create a constraint such that only one row may be 'true'? Rephrased, may I place a WHERE clause in a UNIQUE constraint, or alternatively, may I use a CHECK constraint with an aggregate? Example: CREATE TABLE thing ( thing_id serial primary key, thing_group_id int not null references thing_group(thing_group_id), is_main_thing_p boolean not null default 'f', -- there may be only one main thing per group: unique (thing_group_id, is_main_thing_p='t') -- or else something like: -- check (count (*) from thing where thing_group_id=NEW.thing_group_id and is_main_thing_p = 't' <2) ); Best, Randall