MySQL: Why default NULL instead of empty?
Why would you default a field to NULL instead of just leaving the value empty? Is it merely so you can query IS NULL
?
I'm specif开发者_StackOverflow中文版ically asking about INT
or other numerical data types
Because there's a semantic difference between NULL
and empty.
If a field is empty, how can you tell the difference between the following?
- the value is known to be empty
- the value is unknown
That's what NULL
is for: if you don't know the value then use NULL
; if you know the value -- empty or not -- then use it.
There's no such notion as "empty" in MySQL.
For example, an int column is either an integer, or NULL.
If you don't specify a default value for your int column, if you try to insert a new row without specifying something for this column, mysql will raise an error.
Define "leaving the value empty". An empty value is null. If you mean empty = 0, then think about an account (money wise). You can have an account with 0$ in it, but you also can have no account at all (null). There is a difference between 0 and null.
精彩评论