Detailed MySQL NULL value

1 Overview

MySQL is NULL in a very special value, the official described as "an unknown value", it is not the same value as other data types.
This paper will be explained more particularity angle value NULL.

2. preparation

For demonstration purposes, to create a data table for operation, the following table structure

CREATE TABLE `mytest_null_tbl` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(32) DEFAULT NULL COMMENT 'name value',
  `phone` varchar(32) DEFAULT NULL COMMENT 'phone number',
  `age` tinyint(3) unsigned DEFAULT NULL COMMENT 'age value',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='mytest table for null value';

Then insert a few data

mysql> insert into mytest_null_tbl(name,phone,age) values('Lucy','9299008',18);
Query OK, 1 row affected (0.00 sec)
mysql> insert into mytest_null_tbl(name,phone) values('Taylor',0);    
Query OK, 1 row affected (0.00 sec)
mysql> insert into mytest_null_tbl(name,phone) values('Arwiel','');
Query OK, 1 row affected (0.00 sec)
mysql> insert into mytest_null_tbl(name,phone,age) values('Jenifor','5622890',16);
Query OK, 1 row affected (0.01 sec)

Now look at the data inserted by

mysql> select * from mytest_null_tbl;
+----+---------+---------+------+
| id | name    | phone   | age  |
+----+---------+---------+------+
|  1 | Lucy    | 9299008 |   18 |
|  2 | Taylor  | 0       | NULL |
|  3 | Arwiel  |         | NULL |
|  4 | Jenifor | 5622890 |   16 |
+----+---------+---------+------+

3. NULL value operation

To operate the NULL value is not a mathematical comparison operators, because the comparison with all other value NULL value is FALSE.
The NULL operation, MySQL provided IS NULL and IS NOT NULL, there is a the IFNULL () method.
IS NULL indicates a value determined NULL, IS NOT NULL is exactly the opposite, represents a value is not NULL.

mysql> select * from mytest_null_tbl where age is null;
+----+--------+-------+------+
| id | name   | phone | age  |
+----+--------+-------+------+
|  2 | Taylor | 0     | NULL |
|  3 | Arwiel |       | NULL |
+----+--------+-------+------+
2 rows in set (0.00 sec)

mysql> select * from mytest_null_tbl where age is not null;
+----+---------+---------+------+
| id | name    | phone   | age  |
+----+---------+---------+------+
|  1 | Lucy    | 9299008 |   18 |
|  4 | Jenifor | 5622890 |   16 |
+----+---------+---------+------+
2 rows in set (0.01 sec)

IFNULL (param1, param2), for determining a logical "If the value of a parameter is NULL, then return the value of the parameter 2; otherwise, it returns the value of the parameter a."

mysql> select *,IFNULL(age,0) from mytest_null_tbl;
+----+---------+---------+------+---------------+
| id | name    | phone   | age  | IFNULL(age,0) |
+----+---------+---------+------+---------------+
|  1 | Lucy    | 9299008 |   18 |            18 |
|  2 | Taylor  | 0       | NULL |             0 |
|  3 | Arwiel  |         | NULL |             0 |
|  4 | Jenifor | 5622890 |   16 |            16 |
+----+---------+---------+------+---------------+
4 rows in set (0.02 sec)

Insert and Update value 4.NULL

Normally, NULL values ​​can be plugged into any type of field data, the above table, for example, take a look at the operating results of the update operation varchar, int's.

mysql> update mytest_null_tbl set phone=null,age=null where id=4;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from mytest_null_tbl;               
+----+---------+---------+------+
| id | name    | phone   | age  |
+----+---------+---------+------+
|  1 | Lucy    | 9299008 |   18 |
|  2 | Taylor  | 0       | NULL |
|  3 | Arwiel  |         | NULL |
|  4 | Jenifor | NULL    | NULL |
+----+---------+---------+------+
4 rows in set (0.00 sec)

We can see NULL values are successfully updated to the table.
So, if you do not want the field to be inserted NULL value should be how to do? This time set field properties as NOT NULL can achieve their goals.

mysql> alter table mytest_null_tbl add address varchar(32) not null default '' comment 'address info';
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert into mytest_null_tbl(name,phone,age) values(null,null,null);
Query OK, 1 row affected (0.00 sec)

mysql> insert into mytest_null_tbl(name,phone,age,address) values(null,null,null,null);
ERROR 1048 (23000): Column 'address' cannot be null

mysql> select * from mytest_null_tbl;
+----+---------+---------+------+---------+
| id | name    | phone   | age  | address |
+----+---------+---------+------+---------+
|  1 | Lucy    | 9299008 |   18 |         |
|  2 | Taylor  | 0       | NULL |         |
|  3 | Arwiel  |         | NULL |         |
|  4 | Jenifor | NULL    | NULL |         |
|  5 | NULL    | NULL    | NULL |         |
+----+---------+---------+------+---------+
5 rows in set (0.00 sec)

If some data types, is set to NULL, what would happen then?
Look at the timestamp type.

mysql> alter table mytest_null_tbl add create_time timestamp;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from mytest_null_tbl;
+----+---------+---------+------+---------+---------------------+
| id | name    | phone   | age  | address | create_time         |
+----+---------+---------+------+---------+---------------------+
|  1 | Lucy    | 9299008 |   18 |         | 2019-07-25 15:32:54 |
|  2 | Taylor  | 0       | NULL |         | 2019-07-25 15:32:54 |
|  3 | Arwiel  |         | NULL |         | 2019-07-25 15:32:54 |
|  4 | Jenifor | NULL    | NULL |         | 2019-07-25 15:32:54 |
|  5 | NULL    | NULL    | NULL |         | 2019-07-25 15:32:54 |
+----+---------+---------+------+---------+---------------------+
5 rows in set (0.00 sec)

mysql> update mytest_null_tbl set create_time=null where id=5;                          
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from mytest_null_tbl;
+----+---------+---------+------+---------+---------------------+
| id | name    | phone   | age  | address | create_time         |
+----+---------+---------+------+---------+---------------------+
|  1 | Lucy    | 9299008 |   18 |         | 2019-07-25 15:32:54 |
|  2 | Taylor  | 0       | NULL |         | 2019-07-25 15:32:54 |
|  3 | Arwiel  |         | NULL |         | 2019-07-25 15:32:54 |
|  4 | Jenifor | NULL    | NULL |         | 2019-07-25 15:32:54 |
|  5 | NULL    | NULL    | NULL |         | 2019-07-25 15:34:29 |
+----+---------+---------+------+---------+---------------------+
5 rows in set (0.00 sec)

You can see, when you insert a NULL value for the timestamp type field, MySQL is the latest actual operating time is inserted into the record.
If it is an auto-incremented int type to do this operation will happen then? Look.
Although id column mytest_null_tbl is auto_increment, but set not null, so it is necessary to build a new table to do the test.

mysql> create table mytest_null_tbl2 (
    -> id int unsigned auto_increment,
    -> primary key(id)
    -> )engine=innodb;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from mytest_null_tbl2;
Empty set (0.00 sec)

mysql> insert into mytest_null_tbl2(id) values(null),(null),(null);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from mytest_null_tbl2;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.00 sec)

Can be seen that, upon insertion of the field has a value of NULL AUTO_INCREMENT attribute, MySQL is inserted into the table increment value into the order of the columns.

The merging and sorting

NULL values are not to be involved in distinct, group by, order by the operation, in these operations, all NULL values are considered equal.
Note that, NULL values 0, "" are different, though all three can be expressed as empty, but they are not equal.

mysql> select * from mytest_null_tbl;              
+----+---------+---------+------+---------+---------------------+
| id | name    | phone   | age  | address | create_time         |
+----+---------+---------+------+---------+---------------------+
|  1 | Lucy    | 9299008 |   18 |         | 2019-07-25 15:32:54 |
|  2 | Taylor  | 0       | NULL |         | 2019-07-25 15:32:54 |
|  3 | Arwiel  |         | NULL |         | 2019-07-25 15:32:54 |
|  4 | Jenifor | NULL    | NULL |         | 2019-07-25 15:32:54 |
|  5 | NULL    | NULL    | NULL |         | 2019-07-25 15:34:29 |
+----+---------+---------+------+---------+---------------------+
5 rows in set (0.00 sec)

mysql> select distinct(phone) from mytest_null_tbl;
+---------+
| phone   |
+---------+
| 9299008 |
| 0       |
|         |
| NULL    |
+---------+
4 rows in set (0.00 sec)

In the test data, distinct (phone) group by phone and the result is the same.
ORDER BY In operation, NULL value as the minimum value is present, was at the top when the ASC, the last row at time DESC.

mysql> update mytest_null_tbl set age=20 where id=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update mytest_null_tbl set age=12 where id=5;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from mytest_null_tbl;              
+----+---------+---------+------+---------+---------------------+
| id | name    | phone   | age  | address | create_time         |
+----+---------+---------+------+---------+---------------------+
|  1 | Lucy    | 9299008 |   18 |         | 2019-07-25 15:32:54 |
|  2 | Taylor  | 0       | NULL |         | 2019-07-25 15:32:54 |
|  3 | Arwiel  |         |   20 |         | 2019-07-25 15:50:04 |
|  4 | Jenifor | NULL    | NULL |         | 2019-07-25 15:32:54 |
|  5 | NULL    | NULL    |   12 |         | 2019-07-25 15:50:14 |
+----+---------+---------+------+---------+---------------------+
5 rows in set (0.00 sec)

mysql> select * from mytest_null_tbl order by age;
+----+---------+---------+------+---------+---------------------+
| id | name    | phone   | age  | address | create_time         |
+----+---------+---------+------+---------+---------------------+
|  2 | Taylor  | 0       | NULL |         | 2019-07-25 15:32:54 |
|  4 | Jenifor | NULL    | NULL |         | 2019-07-25 15:32:54 |
|  5 | NULL    | NULL    |   12 |         | 2019-07-25 15:50:14 |
|  1 | Lucy    | 9299008 |   18 |         | 2019-07-25 15:32:54 |
|  3 | Arwiel  |         |   20 |         | 2019-07-25 15:50:04 |
+----+---------+---------+------+---------+---------------------+
5 rows in set (0.00 sec)

mysql> select * from mytest_null_tbl order by age desc;
+----+---------+---------+------+---------+---------------------+
| id | name    | phone   | age  | address | create_time         |
+----+---------+---------+------+---------+---------------------+
|  3 | Arwiel  |         |   20 |         | 2019-07-25 15:50:04 |
|  1 | Lucy    | 9299008 |   18 |         | 2019-07-25 15:32:54 |
|  5 | NULL    | NULL    |   12 |         | 2019-07-25 15:50:14 |
|  2 | Taylor  | 0       | NULL |         | 2019-07-25 15:32:54 |
|  4 | Jenifor | NULL    | NULL |         | 2019-07-25 15:32:54 |
+----+---------+---------+------+---------+---------------------+
5 rows in set (0.00 sec)

6. Statistics and Computing

MySQL for statistics and computing in terms of function, NULL value is based on the differences to achieve different purposes treated.
COUNT (), MIN (), SUM (), they are concerned, NULL is not valid in the calculation are ignored;
and COUNT (*) is a special case, it will calculate all rows, even if there is a NULL value exist, use the time to pay attention to this.

mysql> select * from mytest_null_tbl;
+----+---------+---------+------+---------+---------------------+
| id | name    | phone   | age  | address | create_time         |
+----+---------+---------+------+---------+---------------------+
|  1 | Lucy    | 9299008 |   18 |         | 2019-07-25 15:32:54 |
|  2 | Taylor  | 0       | NULL |         | 2019-07-25 15:32:54 |
|  3 | Arwiel  |         |   20 |         | 2019-07-25 15:50:04 |
|  4 | Jenifor | NULL    | NULL |         | 2019-07-25 15:32:54 |
|  5 | NULL    | NULL    |   12 |         | 2019-07-25 15:50:14 |
+----+---------+---------+------+---------+---------------------+
5 rows in set (0.00 sec)

mysql> select count(*) from mytest_null_tbl;
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

mysql> select count(age) from mytest_null_tbl;
+------------+
| count(age) |
+------------+
|          3 |
+------------+
1 row in set (0.00 sec)

mysql> select min(age) from mytest_null_tbl;       
+----------+
| min(age) |
+----------+
|       12 |
+----------+
1 row in set (0.02 sec)

mysql> select sum(age) from mytest_null_tbl;   
+----------+
| sum(age) |
+----------+
|       50 |
+----------+
1 row in set (0.00 sec)

7. Summary

1) NULL only supports IS NULL, IS NOT NULL, IFNULL () operation;
2) NULL mathematical comparison operators (>, =, <=, <>) calculated result is FALSE;
. 3) is allowed to index column the presence of NULL;
. 4) the DISTINCT, the GROUP BY, in the ORDER BY that all values are equal to NULL;
. 5) that the ORDER BY NULL value is the smallest;
. 6) MIN (), the SUM (), COUNT () in the arithmetic when a NULL value will be ignored, but COUNT (*) is not negligible;
. 7) TIMESTAMP field type is inserted NULL, actually written to the table is the current time;
time field 8) AUTO_INCREMENT attribute is inserted NULL, the actual write into the table is a sequence of self-appreciation;
9) wants to ban a particular field is set to NULL, the field is set for this attribute nOT NULL;
10) if not necessary, do not use NULL, will bring unpredictable Trouble.

Published 105 original articles · won praise 58 · views 410 000 +

Guess you like

Origin blog.csdn.net/ljl890705/article/details/97263432