I have a table called A with these columns:
id, code, unicity
unicity can only have three values 1,2 & 3.
I want to add a unique constraint for a combination of all three columns but with some conditions on unicity.
--first senario:
insert into A values(1,1,1);
insert into A values(1,1,2); --=>ok
--second senario:
insert into A values(1,1,1);
insert into A values(1,1,3);--=>not ok
--third senario:
insert into A values(1,1,1);
insert into A values(1,1,1);--=>not ok
--fourth senario:
insert into A values(1,1,2);
insert into A values(1,1,1);--=>ok
--fifth senario:
insert into A values(1,1,2);
insert into A values(1,1,3);--=>not ok
--sixth senario:
insert into A values(1,1,2);
insert into A values(1,1,2);--=>not ok
When the unicity is 1, if there is already a unicity 1 or 3 with same id and code it should be an error. But if there is already a 2 with same id and code it should be ok.
create index test on A
{
id,
code
case unicity when 1...
}
My question is how to write the case statement for unicity on this case?