Mysql Professional Series - Part 13: God elaborate pit NULL caused people keep track

This is the first 13 series Mysql.

Presentation mysql5.7.25, cmd command: the environment.

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?

Comparison operators use NULL

Look carefully following effects

mysql> select 1>NULL;
+--------+
| 1>NULL |
+--------+
|   NULL |
+--------+
1 row in set (0.00 sec)

mysql> select 1<NULL;
+--------+
| 1<NULL |
+--------+
|   NULL |
+--------+
1 row in set (0.00 sec)

mysql> select 1<>NULL;
+---------+
| 1<>NULL |
+---------+
|    NULL |
+---------+
1 row in set (0.00 sec)

mysql> select 1>NULL;
+--------+
| 1>NULL |
+--------+
|   NULL |
+--------+
1 row in set (0.00 sec)

mysql> select 1<NULL;
+--------+
| 1<NULL |
+--------+
|   NULL |
+--------+
1 row in set (0.00 sec)

mysql> select 1>=NULL;
+---------+
| 1>=NULL |
+---------+
|    NULL |
+---------+
1 row in set (0.00 sec)

mysql> select 1<=NULL;
+---------+
| 1<=NULL |
+---------+
|    NULL |
+---------+
1 row in set (0.00 sec)

mysql> select 1!=NULL;
+---------+
| 1!=NULL |
+---------+
|    NULL |
+---------+
1 row in set (0.00 sec)

mysql> select 1<>NULL;
+---------+
| 1<>NULL |
+---------+
|    NULL |
+---------+
1 row in set (0.00 sec)
    
mysql> select NULL=NULL,NULL!=NULL;
+-----------+------------+
| NULL=NULL | NULL!=NULL |
+-----------+------------+
|      NULL |       NULL |
+-----------+------------+
1 row in set (0.00 sec)
    
mysql> select 1 in (null),1 not in (null),null in (null),null not in (null);
+-------------+-----------------+----------------+--------------------+
| 1 in (null) | 1 not in (null) | null in (null) | null not in (null) |
+-------------+-----------------+----------------+--------------------+
|        NULL |            NULL |           NULL |               NULL |
+-------------+-----------------+----------------+--------------------+
1 row in set (0.00 sec)

mysql> select 1=any(select null),null=any(select null);
+--------------------+-----------------------+
| 1=any(select null) | null=any(select null) |
+--------------------+-----------------------+
|               NULL |                  NULL |
+--------------------+-----------------------+
1 row in set (0.00 sec)
    
mysql> select 1=all(select null),null=all(select null);
+--------------------+-----------------------+
| 1=all(select null) | null=all(select null) |
+--------------------+-----------------------+
|               NULL |                  NULL |
+--------------------+-----------------------+
1 row in set (0.00 sec)

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

Prepare data

mysql> create table test1(a int,b int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test1 values (1,1),(1,null),(null,null);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test1;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    1 | NULL |
| NULL | NULL |
+------+------+
3 rows in set (0.00 sec)

The above three data, serious look, with particular attention to record above-NULL.

IN, NOT IN NULL and compare

IN NULL and compare

mysql> select * from test1;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    1 | NULL |
| NULL | NULL |
+------+------+
3 rows in set (0.00 sec)

mysql> select * from test1 where a in (null);
Empty set (0.00 sec)

mysql> select * from test1 where a in (null,1);
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    1 | NULL |
+------+------+
2 rows in set (0.00 sec)

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

NULL and NOT IN comparison

mysql> select * from test1 where a not in (1);
Empty set (0.00 sec)

mysql> select * from test1 where a not in (null);
Empty set (0.00 sec)

mysql> select * from test1 where a not in (null,2);
Empty set (0.00 sec)
    
mysql> select * from test1 where a not in (2);
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    1 | NULL |
+------+------+
2 rows in set (0.00 sec)

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

EXISTS, NOT EXISTS and compare NULL

mysql> select * from test2;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    1 | NULL |
| NULL | NULL |
+------+------+
3 rows in set (0.00 sec)

mysql> select * from test1 t1 where exists (select * from test2 t2 where t1.a = t2.a);
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    1 | NULL |
+------+------+
2 rows in set (0.00 sec)

mysql> select * from test1 t1 where not exists (select * from test2 t2 where t1.a = t2.a);
+------+------+
| a    | b    |
+------+------+
| NULL | NULL |
+------+------+
1 row in set (0.00 sec)

Copy the table above, we created a table test1 test2.

Query use exists, not exists contrast test1.a = test2.a, because not compare = NULL , consistent results and expectations.

Analyzing only with NULL IS NULL, IS NOT NULL

mysql> select 1 is not null;
+---------------+
| 1 is not null |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

mysql> select 1 is null;
+-----------+
| 1 is null |
+-----------+
|         0 |
+-----------+
1 row in set (0.00 sec)

mysql> select null is null;
+--------------+
| null is null |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)

mysql> select null is not null;
+------------------+
| null is not null |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)

See the above results, the result returned is 0 or 1.

Conclusion: to determine whether the air can only use IS NULL, IS NOT NULL.

NULL aggregate functions pit

Examples

mysql> select count(a),count(b),count(*) from test1;
+----------+----------+----------+
| count(a) | count(b) | count(*) |
+----------+----------+----------+
|        2 |        1 |        3 |
+----------+----------+----------+
1 row in set (0.00 sec)

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.

Continue to see

mysql> select * from test1 where a is null;
+------+------+
| a    | b    |
+------+------+
| NULL | NULL |
+------+------+
1 row in set (0.00 sec)
    
mysql> select count(a) from test1 where a is null;
+----------+
| count(a) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

Sql 1 above using the first query is null a result, in the second sql count (a) is zero return line.

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

NULL values ​​can not serve as the primary key

mysql> create table test3(a int primary key,b int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test3 values (null,1);
ERROR 1048 (23000): Column 'a' cannot be null

Above we have created a table test3, the field ais not specified can not be empty, a NULL data is inserted, the error reasons: a 字段的值不能为NULLwe look at the statement to create the table:

mysql> show create table test3;
+-------+------------+
| Table | Create Table      |
+-------+------------+
| test3 | CREATE TABLE `test3` (
  `a` int(11) NOT NULL,
  `b` int(11) DEFAULT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
+-------+------------+
1 row in set (0.00 sec)

Script can be seen from the above, when the primary key field when the field is automatically set not null.

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.

to sum up

  • 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

Mysql series directory

  1. The first one: mysql Basics
  2. Part 2: Detailed mysql data type (Key)
  3. Part 3: Administrator essential skills (must master)
  4. Part 4: DDL common operations
  5. Part 5: DML operation summary (insert, update, delete)
  6. The first six: select Query Basics
  7. Part 7: Fun select query conditions, avoid mining pit
  8. The first eight: Detailed sorting and paging (order by & limit)
  9. Chapter 9: Detailed grouping queries (group by & having)
  10. The first 10: dozens of commonly used functions Detailed
  11. The first 11: in-depth understanding of the principles and join query
  12. Chapter 12: subquery (very important to master essential)

mysql series of about 20 articles, please look like, welcome to add me or leave a message exchange micro-channel itsoku mysql related technologies!

Guess you like

Origin www.cnblogs.com/itsoku123/p/11582628.html