clear markdown compare help donate comments/suggestions/bugs a leap of faith? diddy dollings
clear markdown donate comments/suggestions/bugs a leap of faith?
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 400790 distinct fiddles created so far.

create table test ( id int primary key, foo bool );
 hidden batch(es)


create function check_one_true(new_foo bool) returns int as $$ begin return ( select count(*) + (case new_foo when true then 1 else 0 end) from test where foo = true ); end $$ language plpgsql stable;
 hidden batch(es)


alter table test add constraint ck_one_true check(check_one_true(foo) = 1);
 hidden batch(es)


insert into test values (1, true); insert into test values (2, false); insert into test values (3, false); insert into test values (4, false);
1 rows affected
1 rows affected
1 rows affected
1 rows affected
 hidden batch(es)


insert into test values (5, true);
ERROR: new row for relation "test" violates check constraint "ck_one_true" DETAIL: Failing row contains (5, t).
 hidden batch(es)


select * from test;
id foo
1 t
2 f
3 f
4 f
 hidden batch(es)


delete from test where id = 1;
1 rows affected
 hidden batch(es)