2

On an Oracle DB, if I have a Nullable column and a check constraint on the same column restricting the values to, say, 'ABC' or 'DEF', can I insert a row with a null value in that column (given that null is not one of the constraint values)?

CodeClimber
  • 4,584
  • 8
  • 46
  • 55

2 Answers2

9

You can read up on check (and other) constraints in the Oracle Concepts manual.

In your case, yes you can insert a row with a null value because the check constraint (e.g. mycolumn in ('ABC', 'DEF') becomes effectively NULL in ('ABC', 'DEF') when mycolumn is null. This Boolean expression will itself evaluate to null, just as null = 'ABC' does, because null is "unknown". And check constraints are only violated if they evaluate to false. Hence your constraint allows you to insert 'ABC', 'DEF' or null into the column.

Tony Andrews
  • 129,880
  • 21
  • 220
  • 259
2

can I insert a row with a null value in that column

Yes you can since you have defined your column as nullable. If you supply a non-null value to the column then the supplied value must match with the values defined in CHECK constraint.

See a live demo here

Rahul
  • 76,197
  • 13
  • 71
  • 125