SQL: Using NULL values vs. default values

前端 未结 13 2234
南旧
南旧 2020-12-01 01:28

What are the pros and cons of using NULL values in SQL as opposed to default values?

13条回答
  •  伪装坚强ぢ
    2020-12-01 01:51

    NULL values are meant to indicate that the attribute is either not applicable or unknown. There are religious wars fought over whether they're a good thing or a bad thing but I fall in the "good thing" camp.

    They are often necessary to distinguish known values from unknown values in many situations and they make a sentinel value unnecessary for those attributes that don't have a suitable default value.

    For example, whilst the default value for a bank balance may be zero, what is the default value for a mobile phone number. You may need to distinguish between "customer has no mobile phone" and "customer's mobile number is not (yet) known" in which case a blank column won't do (and having an extra column to decide whether that column is one or the other is not a good idea).

    Default values are simply what the DBMS will put in a column if you don't explicitly specify it.

提交回复
热议问题