XIII elaborate NULL God caused the pit, people keep track

When the data value is NULL, a variety of unexpected results may occur, people are very hard, we take a look at the various pit NULL lead to God, how to avoid?

First, use the comparison operator NULL

  Any value NULL use the operators (>, <,> =, <=,! =, <>), Or (in, not in, any / some, all) when compared to the value returned is NULL, NULL as a boolean when not 1 not zero.

Two, IN, NOT IN and Comparative NULL  

  IN and NULL comparison:  When comparing IN and NULL, NULL can not check out for the record.

  NULL and NOT IN comparison:  When a NULL value is NOT behind IN, no matter what the circumstances, the entire sql query results are empty.  

Three, EXISTS, NOT EXISTS and compare NULL

  = NULL can not be compared

Fourth, NULL is determined only by IS NULL, IS NOT NULL

  Determining whether empty with only IS NULL, IS NOT NULL.

Five, aggregate functions pit NULL  

  count (a) return two rows, a field is not NULL the statistics out.

  count (b) to return the rows 1, 2 rows to NULL is not counted.

  count (*) you can count all data, regardless of whether the field is NULL.

  SELECT  COUNT (A) from test1 WHERE A IS  null; - returns to 0, as follows:

    count (field) can not count field values ​​that are NULL, count (*) can count the value is null lines.

Six, NULL can not serve as the primary key value  

  Conclusion: When the primary key field when the field is automatically set to not null.

  Read the above these is quite faint, the situation is indeed more difficult to handle NULL, error-prone, the most effective way is to avoid the use of NULL. Therefore, we strongly recommend that you create a field when the field does not allow NULL, set a default value.

Seven summary

  • When NULL as a Boolean value, not 1 nor 0

  • Any use of operators and NULL value (>, <,> =, <=,! =, <>), Or (in, not in, any / some, all), return value is NULL

  • When comparing the IN and NULL, NULL can not check out for the record

  • When a NULL value is NOT behind IN, no matter what the circumstances, the entire sql query results are empty

  • Determining whether empty with only IS NULL, IS NOT NULL

  • count (field) can not count field values ​​that are NULL, count (*) can count the value is null lines

  • When the primary key field when the field is automatically set to not null

  • NULL lead to pit people are very hard, it is strongly recommended that you create a field when the field does not allow NULL, to a default value

Guess you like

Origin www.cnblogs.com/biao/p/11764981.html