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"
Re: UNIQUE boolean: only one row may be "true" Re: UNIQUE boolean: only one row may be "true" |
Список | 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 по дате отправления: