Morning all,
Hmmmm, no-one answered the questions!
As Ed noted, NULL doesn't play nice with values, other than the empty
string of course '' - but that's one of those "three foot tall judges"
of the Oracle world. (Little things, sent to try us!). All you can do
with a NULL is "IS NOT NULL" or "IS NULL" - that is it, no more, nada,
nothing at all. Don't even think about it!
Here we go:
ALTER
ADD CONSTRAINT
CHECK (some_column in ('Y','N',NULL));
-
Is the value 'Y' allowed?
Yes.
-
Is the value 'N' allowed?
Yes.
-
Is the non-value NULL allowed?
Yes.
-
Is the value 'X' allowed?
Yes.
-
How about single characters in lower case, are they allowed?
Yes.
And a bonus (unasked question):
6. What is rejected by the constraint?
NOTHING AT ALL.
Explain your answers!
Ok, here's the deal. When an invalid constraint expression like the
above is executed, the constraint value appears to be NULL. That is not
a true or false (as in "can I put this value in here?") so the
constraint doesn't fail and the value in question is allowed through.
Example:
(Sorry Bert, I'm using SQL*Plus here!!!)
SQL> create table oops(a varchar2(1));
Table created.
SQL> alter table oops add constraint major_fail
2 check (a in ('Y','N', NULL));
Table altered.
SQL> insert into oops values (null);
1 row created.
SQL> insert into oops values ('Y');
1 row created.
SQL> insert into oops values ('N');
1 row created.
SQL> insert into oops values ('X');
1 row created.
SQL> insert into oops values ('a');
1 row created.
SQL> select a, case when a in ('Y','N') then 'DATA OK' else 'DATA
CORRUPT!' end as result
2 from oops
3 where a is not null;
A RESULT