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