UNIQUE boolean: only one row may be "true" - Mailing list pgsql-sql

From Randall Lucas
Subject UNIQUE boolean: only one row may be "true"
Date
Msg-id FA25F27B-7FDB-11D7-A71E-000A957653D6@tercent.net
Whole thread Raw
Responses Re: UNIQUE boolean: only one row may be "true"
Re: UNIQUE boolean: only one row may be "true"
Re: UNIQUE boolean: only one row may be "true"
List pgsql-sql
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



pgsql-sql by date:

Previous
From: "alexandre :: aldeia digital"
Date:
Subject: pg_stat_activity IP address
Next
From: "Diehl, Jeffrey"
Date:
Subject: Re: pgsql Replication Proxy (was Re: Replication for a