New answers tagged

3

In Postgres, you could use the basic table definitions you have (slightly different syntax) and add a unique functional index using COALESCE(): CREATE UNIQUE INDEX entity_4attr_uni ON entity(attr_a_id , attr_b_id , COALESCE(attr_c_id, 0) , ...


3

There isn't an easy way (except in SQL Server, see below) to enforce these constraints. And I say "these" and not "this" because they are indeed more than one. You want to enforce uniqueness on (a,b,c,d) when all the attributes are not null. And uniqueness on (a,b,c) when d is null. And uniqueness on (a,b,d) when c is null. And uniqueness on (a,b) when both ...



Top 50 recent answers are included