What is the difference between “Is Not Null” and “Not Is Null”

后端 未结 3 1163
攒了一身酷
攒了一身酷 2021-02-02 08:49
SELECT id FROM customers WHERE type IS NOT Null;

Versus:

SELECT id FROM customers WHERE NOT type IS NULL;

The data th

3条回答
  •  情歌与酒
    2021-02-02 08:58

    In the usual case where the LHS term is a simple variable or expression, there is no difference between NOT x IS NULL and x IS NOT NULL. The optimizer will treat the two identically.

    However, in full SQL, the LHS term is not limited to being a simple variable or expression; in the formal grammar, the LHS is a :

    SQL/Foundation - ISO/IEC 9075-2:2003

    §8.7 (p395)

    Specify a test for a null value.

     ::=  
    
     ::= IS [ NOT ] NULL
    

    And chasing through the grammar, you find that:

    §7.2 (p296)

    Specify a row value.

    [...]

      ::=
           
     |     
    
     ::=  
    

    And:

    §7.1 (p293)

    Specify a value or list of values to be constructed into a row or partial row.

     ::=
           
     |     
     |     
    

    [...]

     ::=
           
     |     
     |     
    

    And so it goes on. (Chasing anything through the SQL standard is hard work. You can find a heavily hyperlinked version of the standard at http://savage.net.au/SQL/.)

    However, as you may guess from the mention of 'row value', you can have multiple simple expressions combined on the LHS to form a 'row value constructor predicand'. And then there is a difference between the two forms.

    Conceptually, you have:

    (val1, val2, val3) IS NOT NULL
    

    vs

    NOT (val1, val2, val3) IS NULL
    

    Now, in the first case, you get TRUE if each of val1, val2 and val3 is not NULL. In the second case, you get TRUE if any one of val1, val2, val3 is not NULL. So, there are circumstances where the two operations are not identical.

    However, as I said up front, for the usual case of a simple column or expression, there is no difference between the two.

提交回复
热议问题