According to null-values, the value of null in MySQL just means that there is no data. The null value is different from the zero value of some types. For example, the zero value of int type is 0, and the zero value of string is "", but they still have data, not null. When we save data, we habitually record the temporarily unavailable data as null, indicating that we cannot provide valid information at present. However, when using null, we need to pay attention to some problems. The MySQL documentation explains this as follows: problems-with-null Error-prone points when using nullBelow I will explain the error-prone points of null given by MySQL officials. People who are not familiar with MySQL can easily confuse null and zero values.
For example, the data generated by the following two SQL statements are independent mysql> INSERT INTO my_table (phone) VALUES (NULL); mysql> INSERT INTO my_table (phone) VALUES (''); The first SQL statement only indicates that the phone number is not known at the moment, and the second statement indicates that the phone number is known and recorded as
Logical judgment of null should be handled separately To determine whether a value is null, you must use the special syntax
If you use = to judge, it will always be false
For example, if you write it like this, the result of the where test will never be true: SELECT * FROM my_table WHERE phone = NULL; If you use null and other data to do calculations, the result will always be null, unless the MySQL documentation makes additional special instructions for certain operations.
For example: mysql> SELECT NULL, 1+NULL, CONCAT('Invisible',NULL); +------+--------+--------------------------+ | NULL | 1+NULL | CONCAT('Invisible',NULL) | +------+--------+--------------------------+ | NULL | NULL | NULL | +------+--------+--------------------------+ 1 row in set (0.00 sec) So if you want to make a logical judgment on null, just use To look for NULL values, you must use the IS NULL test Indexing columns with null values requires additional anticipation of hidden detailsOnly InnoDB, MyISAM, and MEMORY storage engines support indexing columns with null values.
The length of the index will be 1 larger than the normal index, which means it consumes slightly more memory. Due to the key storage format, the key length is one greater for a column that can be NULL than for a NOT NULL column. Grouping, deduplication, and sorting of null values will be treated speciallyContrary to the above statement that =null is always false, null is considered equal.
Sorting null values is treated specially.Null values are either sorted first or last.
Null is ignored during aggregation operations
For example, count(*) will not count data with a null value.
mysql> SELECT COUNT(*), COUNT(age) FROM person; The above are the details of some common mistakes in MySQL null. For more information about MySQL null, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Things to note when writing self-closing XHTML tags
>>: 18 killer JavaScript one-liners
In actual work or interviews, we often encounter ...
This article uses an example to describe how MySQ...
Query the MySQL source first docker search mysql ...
The current environment is: Centos 7.5 docker-ce ...
1. left(name,4) intercepts the 4 characters on th...
Table of contents Method 1: Call the function dir...
Floating ads are a very common form of advertisin...
1. Create table statement: CREATE TABLE `employee...
Table of contents 1. Introduction 2. The first me...
Table of contents Single-machine deployment Onlin...
In Ubuntu, you often encounter the situation wher...
What does text-fill-color mean? Just from the lit...
Table of contents 1. Introduction 2. Use 1. Diffe...
SQL paging query:background In the company's ...
vmware vsphere 6.5 is the classic version of vsph...