Let's assume I have a table:
CREATE TABLE "STACK_OVERFLOW_QUESTION"
(
"SSN" VARCHAR2(13 BYTE) NOT NULL ENABLE,
"IRN" VARCHAR2(11 BYTE) NOT NULL ENABLE,
"BEGIN_DATE" DATE,
"END_DATE" DATE
)
I need a unique index which will enforce a policy where there cannot be intersection between the intervals from BEGIN_DATE to END_DATE having the same SSN and IRN. For example, let' assume there are these rows in the table:
| SSN | IRN | BEGIN_DATE | END_DATE |
|------------|-------------|--------------|-------------|
| 565654 | 154646678 | 01/01/2010 | 01/02/2010 | - (first row inserted) OK
| 565654 | 154646678 | 03/04/2010 | 20/04/2010 | - (second row inserted) OK
| 565654 | 154646678 | 28/01/2010 | 13/02/2010 | - (third row inserted) ERROR
Unique index is violated in the third row. Because intervals from 28/01/2010
to 13/02/2010
intersects with 01/01/2010
and 01/02/2010
.
What is the most efficient way for handling this?