Oracle SQL Constraint with IF condition

Viewed 40

I need create a constraint with this condition If column_1 = 'Y' then column_2 is not null. I need make the column_2 not null if column_1 is equal 'Y'. In other cases you can insert or not values in column_2

1 Answers

You can express this as:

check (column_1 <> 'Y' or column_2 is not null)

Note: This version assumes that column_1 is not NULL, but the logic can easily be adjusted to handle that.

Or, alternatively:

check (not (column_1 = 'Y' and column_2 is null) )
Related