Is there a way to ignore a unique constraint if a given condition is true?
For example I got 3 columns in my database that form a unique constraint:
create table example_table
(
column_primarykey RAW(16) default NULL not null,
column_a number(8) not null,
column_b number(8) not null,
column_c number(8) not null,
constraint constraint_1
unique(column_a, column_b, column_c)
constraint constraint_2
primary key (column_primarykey)
Now I add a fourth column:
alter table example_table
add column_d number(8) not null,
What I want to achieve is, that the unique constraint gets ignored if the value of column_d already exists in the table. If column_d is not unique in the table the unique constraint gets ignored and you can add the row to the table. For example this is the existing data in my table (ignoring primary key cause not relevant):
| column_a | column_a | column_c | column_d |
|---|---|---|---|
| 1 | 2 | 3 | 1 |
| 3 | 4 | 5 | 2 |
So want I want is that you can add for example (1, 2, 3, 1) but not (1, 2, 3, 2) since there is already a row with the first three values. It should be only possible if the value in column_d already exists and the other values are equal to the existing row.
More examples to help with understanding:
| Example insert | result | reason |
|---|---|---|
| (1, 2, 3, 1) | accepted | d is not unique and a, b, c got same values as the existing row with value 1 for column_d |
| (1, 2, 3, 4) | rejected | a, b ,c exists already in the table |
| (5,6,7,1) | rejected | 1 exists but with different values for a b and c |
| (3,4,5, 2) | accepted | d exists and a, b, c, have the same values |
| (7,8,9,3) | accepted | a, b, c are unique and d does not exist |
column_dalready exists in the table. - Serg