开发者

MySQL: Why default NULL instead of empty?

开发者 https://www.devze.com 2023-02-15 20:37 出处:网络
Why would you default a field to NULL instead of just leaving the value empty?Is it merely so you can query IS NULL ?

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?

  1. the value is known to be empty
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消