Allen said:
Here's a list of the 6 most common mistakes people make with Null. It may
help you to understand what you have done to cause the error:
Common errors with Null
at:
http://members.iinet.net.au/~allenbrowne/casu-12.html
Interesting article. May I suggest an addendum to you Error 1 ('Nulls
in Criteria') description?
Using the Customers table from Northwind, here's a summary of what you
have so far:
SELECT DISTINCT
(
SELECT COUNT(*) FROM Customers) AS Total,
(
SELECT COUNT(*) FROM Customers WHERE Region = 'BC') AS is_BC,
(
SELECT COUNT(*) AS Counts FROM Customers WHERE Region <> 'BC') AS
not_BC,
(
SELECT COUNT(*) AS Counts FROM Customers WHERE Region IS NULL) AS
is_null FROM customers
;
Consider this in isolation:
SELECT COUNT(*) FROM customers WHERE Region <> 'BC';
My Northwind is unaltered so the query returns the value 29. Let's use
this in an arbitrary CHECK constraint on another table:
ALTER TABLE Products
ADD CONSTRAINT ch__DopMe1
CHECK (50 < (
SELECT COUNT(*) FROM customers WHERE Region <> 'BC')
)
;
Now let's try to add a new row into the products table:
INSERT INTO Products (ProductName, Discontinued)
VALUES ('DeleteMe1',0)
;
This INSERT succeeds whereas your description says it should fail. To
demonstrate the point, let's apply a second CHECK constraint, replacing
the query with the literal value of 29:
ALTER TABLE Products
ADD CONSTRAINT ch__DopMe2
CHECK (50 < 29)
;
And try a second INSERT:
INSERT INTO Products (ProductName, Discontinued)
VALUES ('DeleteMe2',0)
;
This time, the second CHECK constraint bites and the INSERT fails. Why
not the first?
The answer is that your description of 'Nulls in Criteria' only applies
only to SQL DML (Data Modification Language e.g. SELECT syntax).
Unknown values are treated differently in SQL DDL (Data Definition
Language e.g. ALTER TABLE). In a DDL WHERE clause, the unknown result
of an expression does not exclude the row from the results set.
I am aware that this discussion goes beyond the needs of the 'Casual
User'; most 'Power' users seem unaware of the existence of the CHECK
constraint syntax. However, I think the fact that 'nulls in criteria'
is handled differently for DDL is due at least a mention.
Jamie.
--