Why doesn't SQL support “= null” instead of “is null”?

前端 未结 12 2051
不知归路
不知归路 2020-11-29 11:11

I\'m not asking if it does. I know that it doesn\'t.

I\'m curious as to the reason. I\'ve read support docs such as as this one on Working With Nu

12条回答
  •  清歌不尽
    2020-11-29 11:28

    I can't help but feel that you're still not satisfied with the answers that have been given so far, so I thought I'd try another tack. Let's have an example (no, I've no idea why this specific example has come into my head).

    We have a table for employees, EMP:

    EMP
    ---
    EMPNO           GIVENNAME
    E0001           Boris
    E0002           Chris
    E0003           Dave
    E0004           Steve
    E0005           Tony
    

    And, for whatever bizarre reason, we're tracking what colour trousers each employee chooses to wear on a particular day (TROUS):

    TROUS
    -----
    EMPNO       DATE        COLOUR
    E0001       20110806    Brown
    E0002       20110806    Blue
    E0003       20110806    Black
    E0004       20110806    Brown
    E0005       20110806    Black
    E0001       20110807    Black
    E0003       20110807    Black
    E0004       20110807    Grey
    

    I could go on. We write a query, where we want to know the name of every employee, and what colour trousers they had on on the 7th August:

    SELECT e.GIVENNAME,t.COLOUR
    FROM
        EMP e
            LEFT JOIN
        TROUS t
            ON
                 e.EMPNO = t.EMPNO and
                 t.DATE = '20110807'
    

    And we get the result set:

    GIVENNAME       COLOUR
    Chris           NULL
    Steve           Grey
    Dave            Black
    Boris           Black
    Tony            NULL
    

    Now, this result set could be in a view, or CTE, or whatever, and we might want to continue asking questions about these results, using SQL. What might some of these questions be?

    1. Were Dave and Boris wearing the same colour trousers on that day? (Yes, Black==Black)

    2. Were Dave and Steve wearing the same colour trousers on that day? (No, Black!=Grey)

    3. Were Boris and Tony wearing the same colour trousers on that day? (Unknown - we're trying to compare with NULL, and we're following the SQL rules)

    4. Were Boris and Tony not wearing the same colour trousers on that day? (Unknown - we're again comparing to NULL, and we're following SQL rules)

    5. Were Chris and Tony wearing the same colour trousers on that day? (Unknown)

    Note, that you're already aware of specific mechanisms (e.g. IS NULL) to force the outcomes you want, if you've designed your database to never use NULL as a marker for missing information.

    But in SQL, NULL has been given two roles (at least) - to mark inapplicable information (maybe we have complete information in the database, and Chris and Tony didn't turn up for work that day, or did but weren't wearing trousers), and to mark missing information (Chris did turn up that day, we just don't have the information recorded in the database at this time)

    If you're using NULL purely as a marker of inapplicable information, I assume you're avoiding such constructs as outer joins.


    I find it interesting that you've brought up NaN in comments to other answers, without seeing that NaN and (SQL) NULL have a lot in common. The biggest difference between them is that NULL is intended for use across the system, no matter what data type is involved.

    You're biggest issue seems to be that you've decided that NULL has a single meaning across all programming languages, and you seem to feel that SQL has broken that meaning. In fact, null in different languages frequently has subtly different meanings. In some languages, it's a synonym for 0. In others, not, so the comparison 0==null will succeed in some, and fail in others. You mentioned VB, but VB (assuming you're talking .NET versions) does not have null. It has Nothing, which again is subtly different (it's the equivalent in most respects of the C# construct default(T)).

提交回复
热议问题