Вы можете использовать unique index
с условием. посмотрите следующий пример:
SQL> create table test123 (col1 number, col2 number, col3 number);
Table created.
SQL> -- You need something like this (solution)
SQL> create unique index test123_ix01 on test123(case when col3 in (1,2) then col1 end,
2 case when col3 in (1,2) then col2 end,
3 case when col3 in (1,2) then col3 end);
Index created.
SQL>
Теперь давайте проверим, работает ли он или нет:
SQL> insert into test123 values (1,2,3);
1 row created.
SQL> insert into test123 values (1,2,3);
1 row created.
SQL> insert into test123 values (1,2,1);
1 row created.
SQL> insert into test123 values (1,2,2); -- this is of your interest -- see col3 value
1 row created.
SQL> insert into test123 values (1,2,2); -- this is of your interest -- see col3 value
insert into test123 values (1,2,2)
*
ERROR at line 1:
ORA-00001: unique constraint (TEJASH.TEST123_IX01) violated
SQL>
SQL> select * from test123;
COL1 COL2 COL3
---------- ---------- ----------
1 2 3
1 2 3
1 2 1
1 2 2
SQL>
Вау !!! Он ограничил множественные значения col3
, когда col3
равно 2, и будет вести себя так же, если col3
равно 1. Все остальные статусы разрешено вставлять несколько раз.
Cheers !!