This is a toy example that illustrates a real problem in PostgreSQL. The below examples are using a PostgreSQL 8.4.3 server, but I suspect other versions have the same problem.
Given the following table:
=> create table tmp_foo (foo boolean not null unique, bar boolean not null unique); => insert into tmp_foo (foo, bar) values (true, true), (false, false); => select * from tmp_foo; foo | bar -----+----- t | t f | f
can the table be modified to look like this:
=> select * from tmp_foo; foo | bar -----+----- t | f f | t
without deleting rows or modifying the table schema? This:
=> update tmp_foo set bar = not bar; ERROR: duplicate key value violates unique constraint "tmp_foo_bar_key"
does not work.
If deletes are allowed, this:
=> create temp table tmp_foo_2 as select * from tmp_foo; => update tmp_foo_2 set bar = not bar; => delete from tmp_foo; => insert into tmp_foo select * from tmp_foo_2;
works. It is not the simplest solution for this example, but it is easily generalizable to more complex examples.