UNIQUE boolean: only one row may be "true"

Поиск
Список
Период
Сортировка
От Randall Lucas
Тема UNIQUE boolean: only one row may be "true"
Дата
Msg-id FA25F27B-7FDB-11D7-A71E-000A957653D6@tercent.net
обсуждение исходный текст
Ответы Re: UNIQUE boolean: only one row may be "true"  (Michael Teter <michael_teter@yahoo.com>)
Re: UNIQUE boolean: only one row may be "true"  (Bruno Wolff III <bruno@wolff.to>)
Re: UNIQUE boolean: only one row may be "true"  (chester c young <chestercyoung@yahoo.com>)
Список 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 по дате отправления:

Предыдущее
От: "alexandre :: aldeia digital"
Дата:
Сообщение: pg_stat_activity IP address
Следующее
От: "Diehl, Jeffrey"
Дата:
Сообщение: Re: pgsql Replication Proxy (was Re: Replication for a