How can I avoid NULLs in my database, while also representing missing data?

前端 未结 8 1986
予麋鹿
予麋鹿 2020-12-12 14:19

In SQL and Relational Theory (C.J. Date, 2009) chapter 4 advocates avoiding duplicate rows, and also to avoid NULL attributes in the data we store. While I have

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 14:39

    NULL could/should be used as long as:

    A) You have a business reason. For example, in a table of payments, a NULL payment value would mean it was never paid. A 0.00 payment value would mean we intentionally paid nothing. For medical charts, a NULL value for a blood pressure reading would mean you didn't take a BP, a 0 value would mean the patient is dead. This is a significant distinction, and necessary in certain applications.

    B) Your queries account for it. If you understand the affect of NULL on IN, EXISTS, inequality operators (like you specified in OP), etc. then it shouldn't be an issue. If you have NULL now in your tables and don't want the value for certain applications, you can employ views and either COALESCE or ISNULL to populate different values if the source table has a NULL.

    EDIT:

    To address OP's questions about "real world" inequalities/equalities using NULL, this is a great example I use sometimes.

    You are at a party with 3 other people. You know that one person is named "John" but don't know the others.

    Logically, the answer for "How many people are named Joe" is unknown or NULL. In SQL, this would be something like

    SELECT name FROM party where NAME = 'Joe' You would get no rows since you don't know their names. They may or may not be Joe.

    Your inequality would be:

    SELECT name from party where NAME <> 'Joe' You would only get a return value for "John" since John's name is all you know. The other people may or may not be Joe, but you have no way to know.

提交回复
热议问题