Reputation: 39075
Consider a table that has a unique constraint for fields a,b and c. For example something like
create table my_tbl (
a number,
b number,
c varchar2(10),
...
constraint constr_name_u unique(a,b,c)
);
Now I want to modify the constraint such that rows where b < a does not need to be unique. That means that I want to allow non-uniqueness only for rows where b < a. All other rows still have to be unique.
How can I create such a 'relaxed' unique constraint?
Upvotes: 1
Views: 118
Reputation: 231671
You can't code that logic in a constraint.
You can create a unique function-based index, however, which probably lets you accomplish the same task
CREATE UNIQUE INDEX idx_my_table
ON my_table ( CASE WHEN a <= b THEN a ELSE NULL END,
CASE WHEN a <= b THEN b ELSE NULL END,
CASE WHEN a <= b THEN c ELSE NULL END );
This takes advantage of the fact that Oracle doesn't store completely NULL
values in the index structure. This allows you to have as many rows where b < a as you'd like and none of them would be stored in the index structure.
Upvotes: 3