• Adolfo J. Socorro


    main cause of confusion, I would say, is thinking that NULL means blank or empty.

    Microsoft


    A value of NULL indicates that the value is unknown. A value of NULL is different from an empty or zero value.

    If that is true then, what is an empty value? AFAIK, NULL is the only way to leave a field empty!

    I think it would be more reasonable to say that from the point of view of a logical or arithmetic operation a NULL value is considered unknown since it cannot be resolved to an actual value so it is unknown within the context of the operation and that would make the result of the operation unknown as well, but from the point of view of data storage it is actually a blank or empty field.

    Socorro


    One way to avoid worrying about NULLs is never to use them, always declaring columns as not allowing NULLs and designating default values for "empty" or "unknown". This will save you keystrokes, especially when you want to check whether a column does not have a certain value.

    I don't see how that will save you significant amount of time, checking for a NULL is just as easy as checking for any value and it will make your code more readable. Using a magic number is something you would want to do if you didn't had NULL support, it doesn't have any advantages over NULLs (you'll still have to check for the magic value implicitly to find out if it was set), and it has some disadvantages, of the top of my head:

    1) It can screw up greater than/less than queries (the empty fields may come up on the query when they're not supposed to);

    2) If you do it on a foreign key then you'll need a dummy record on the referenced table;

    3) Most front-end data frameworks can handle NULL values without any special handling, for example you can store a NULL value in any nullable data type or use it to set a GUI control directly, a magic value will always require some special handling;

    4) Any qualified developer should understand the concept of NULLs but may not understand the logic behind your magic value.