I’ve seen this on a lot of fields on a DB from a project I’ve been working on, where a column will be defined not null, but will have an empty string as the default. what’s the point of doing this? If you will allow an empty string, why not just allow a field to be null?
Share
NULLs have special behavior: comparing anything with a NULL gives you back a
NULL, which is something else thanfalseor0. It means “unknown”.For example, take this table:
SELECT * FROM mytable WHERE gender = 'M'will return 1 row, as expectedSELECT * FROM mytable WHERE gender != 'M'will return 2 rows, NOT 3 rows.SELECT * FROM mytable WHERE gender != 'M' OR gender IS NULLwill return the expected 3 rows.Edit: For some applications, using
0(or, God forbid, another “magic number”) instead ofNULLis not even advisable (units or exact values are not relevant in this example):Here, the
NULLon Jan 6th means “value unknown” – maybe because the temperature was so low that the thermometer probe stopped responding. However, it’s a completely different meaning than Jan 3rd, when the temperature was0, that is, 0 degrees.Also, as @Bill Karwin mentions, NULLs behave specially in aggregate functions (
COUNT,SUM,AVGetc.): calculatingAVG(Temperature)on the above data would give you-14.5, as the NULL row is ignored.