Re: Clarification on NULL Records

From: David Portas (REMOVE_BEFORE_REPLYING_dportas_at_acm.org)
Date: 05/10/04


Date: Mon, 10 May 2004 07:26:48 +0100

NULL represents a missing/unknown attribute rather than an actual value. The
basic comparison operators like =, <>, <, >, IN, etc return an UNKNOWN
result when NULLs are involved and comparisons that return UNKNOWN are
excluded by the WHERE clause.

Read about NULLs and three-value logic in Books Online:

http://msdn.microsoft.com/library/en-us/acdata/ac_8_qd_02_8pwy.asp

Use the IS NULL / IS NOT NULL predicate to test for the presence or absence
of NULLs:

SELECT *
 FROM table1
 WHERE required <> 'Y'
  OR required IS NULL

It isn't good practice to use NULL to represent a known value of an
attribute because it makes your code more complex and may prevent the
optimizer from using an index to speed up your queries. For example if your
"Required" column was intended to represent the values of "Y" and "N" then
better declare it like this:

CREATE TABLE dbo.Table1 (names NVARCHAR (50),required CHAR(1) NOT NULL CHECK
(required IN ('Y','N')) DEFAULT 'N')

Notice the use of a DEFAULT to avoid NULLs. Now you can write:

SELECT *
 FROM table1
 WHERE required = 'N'

-- 
David Portas
SQL Server MVP
--


Relevant Pages

  • Re: So whats null then if its not nothing?
    ... two nulls are not distinct; ... it means effectively that unknown is treated as ... constraints leads to tuples being rejected if false; ... The SQL unique constraint does not consider two ...
    (comp.databases.theory)
  • Re: Null and not equal in Join Condition
    ... Hi Robert, ... NULLS or not. ... Any comparison to a NULL value will always be "unknown". ... equal to a value that's not in the database. ...
    (microsoft.public.sqlserver.programming)
  • Re: So whats null then if its not nothing?
    ... >> According to the 3vl rules, whether two nulls are distinct or equal is ... it means effectively that unknown is treated as ... Clearly, you have a duplicate. ... the result is unknown and therefore the constraint is not violated. ...
    (comp.databases.theory)
  • Re: Why does 1:3 relationsihp require another table?
    ... What is meaning for NULL in the ... > column for employees who are non-commissioned? ... Really the set of NULLs does represent an empty set. ... shipdate for a shipment could be unknown. ...
    (microsoft.public.sqlserver.programming)
  • Re: Why does 1:3 relationsihp require another table?
    ... practically difficult if not impossible and that is why NULLs or any such ... If representation of unknown value is the idea behind the ... implementation of NULLs in SQL, then SQL avoids far too many kinds of ... By assuming support for just one kind of missing information (Unknown value ...
    (microsoft.public.sqlserver.programming)