Skip to Main Content

Oracle Database Free

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to apply a conditional constraint

PericlesJan 22 2025

Hello

I have a table that has six columns. If the fourth column is null, then the first three columns, which are not nullable, must be unique, but if the fourth column is not null, then all the four columns must be unique.

Example of allowed cases:

COL1 COL2 COL3 COL4 COL5 COL6

1 1 1 NULL 123 125

1 1 2 NULL 123 125

1 2 1 NULL 123 125

2 1 1 NULL 123 125

1 1 1 1 123 125

Example of not allowed cases

COL1 COL2 COL3 COL4 COL5 COL6

1 1 1 1 123 125

1 1 1 1 123 125 INSERT/UPDATE MUST FAIL

1 2 1 NULL 123 125

1 2 1 NULL 123 125 INSERT/UPDATE MUST FAIL

There is a way to create a constraint that prevent any insertion in the table that do not accomplish these conditions?

Thanks

This post has been answered by Solomon Yakobson on Jan 22 2025
Jump to Answer
Comments
Post Details
Added on Jan 22 2025
4 comments
89 views