X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id BA333475CBC for ; Tue, 6 May 2003 13:13:51 -0400 (EDT) Received: from web10001.mail.yahoo.com (web10001.mail.yahoo.com [216.136.130.37]) by postgresql.org (Postfix) with SMTP id 6FC59474E42 for ; Tue, 6 May 2003 13:13:50 -0400 (EDT) Message-ID: <20030506171349.3277.qmail@web10001.mail.yahoo.com> Received: from [204.155.157.33] by web10001.mail.yahoo.com via HTTP; Tue, 06 May 2003 10:13:49 PDT Date: Tue, 6 May 2003 10:13:49 -0700 (PDT) From: Michael Teter Subject: Re: UNIQUE boolean: only one row may be "true" To: Randall Lucas , pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Spam-Status: No, hits=-26.0 required=5.0 tests=BAYES_01,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REPLY_WITH_QUOTES 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/93 X-Sequence-Number: 13258 Can you elaborate a little on what you want to do with this (as in, example data, problem domain, etc.)? This is probably not the answer you're looking for, but without knowing more, I would guess that you need a different design, not a 1-true,many false field constraint. Perhaps you want something like this? Table A: thing_groupid (primary key) ...other group info... Table B: thing_groupid (reference to A.thing_groupid, many to 1) thing_id ...details about thing... ? MT --- Randall Lucas wrote: > > 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 > > > ---------------------------(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 __________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com