If you are dealing with NULL
in the database, then you should consider the expressions as "trinary logic". That is, a boolean value does not evaluate to two values anymore (as in binary), but to three:
- True.
- False.
- Null: The SQL Standard is not clear about its definition, but it may mean the total absence of value, that the value is unknown, or it cannot be determined.
Now, take notice the CHECK
constraint and the WHERE
clause behave differently regarding null values. Consider the following example:
create table t (
a int check (a <> 5)
);
insert into t (a) values (1), (2), (null);
The WHERE clause
A WHERE
clause accepts a row if the predicate (the expression that represents the condition) is "true"; it rejects the row if the predicate evaluates to "false" or to "null".
select * from t where a = 1;
Result:
a
-
1
As you see, "null" was excluded from the result set since the predicate a = 1
evaluated to "null".
The CHECK Constraint
The CHECK
constraint works a little bit different than WHERE
. The predicate is rejected when it evaluates to "false" only; it's accepted when it evaluates to "true" and also "null".
That's why it inserted all three rows, including the "null" value.
select * from t;
Result:
a
----
1
2
null
For the "null" value, the predicate a <> 5
evaluated to "null" so it was accepted, and the row was inserted.