The 5 big pits where the MySQL field is NULL, most people have stepped on

Have you ever encountered the problem that database fields allow null values?

Before verifying the problem, we first build a test table and test data.

 The constructed test data is shown in the figure below:

 With the above table and data, let's see what problems will be caused when there are NULL values ​​in the column?

1.count data lost

We all know that count is used for counting. When there is a NULL value in a certain field in the table, the data calculated by count will be lost, as shown in the following SQL :

 The query execution results are as follows:

 It can be seen from the above results that the values ​​of count(*) and count(name) are different, that is, when the count(name) query is used, two data whose value is NULL are lost .

solution

If there is a NULL value in a column , use count(*) for data statistics.

Extended knowledge: don't use count ( constant )

Explanation: count(*) will count the rows whose value is NULL , but count ( column name ) will not count the rows whose value is NULL .

2.distinct data loss

When using the statement count(distinct column1,column2) , if a field value is empty, even if another column has a different value, the result of the query will lose data. The SQL is as follows:

 

The query execution results are as follows:

 The original data of the database is as follows:

 As shown in the above figure, the 10 pieces of data in the mobile column are all different, but the result of the query is only 8 pieces.

3.select data loss

If there is a NULL value in a certain column , if you execute a non-equal query ( <> or != ), the result of the NULL value will be lost, such as the following data:

 When we query all the data whose name is not equal to "Java" , the expected result should be the data with id from 2 to 10 , but when executing the following SQL query:

 The query results are as follows:

 It can be seen that the two data whose name is NULL with id=9 and id=10 are not queried, and this result does not meet our normal expectations.

solution

To solve the above problems, just modify the conditions to check out the names that are not equal to Java or null values, and execute the SQL as follows:

 The execution results are as follows:

 It can be seen that all 10 pieces of data have been queried, and this result is in line with our normal expectations.

4. Causes a null pointer exception

When we use some functions, such as the sum function sum(column) or the average value, if there is a null value in the requested field, the requested value will be empty instead of 0 .

If the result of the sum query is NULL , it may cause a null pointer exception ( NPE ) during program execution. Let's demonstrate this problem.

To demonstrate this problem, first we build a table and some test data:

 The raw data in the table are as follows:

 Next we use the sum query, executing the following SQL :

 The query execution results are as follows:

 When the result of the query is NULL instead of 0 , it can cause a null pointer exception.

Solve Null Pointer Exception

You can use ifnull() to handle null values ​​to avoid null pointer exceptions:

 The query execution results are as follows:

 5. Increased query difficulty

When there is a null value in the field, the difficulty of querying null or non- null values ​​increases. You must use a query method that matches null , such as IS NULL or IS NOT NULL or an expression such as IFNULL (cloumn). Query, the traditional = , != , <>... these expressions cannot be used, which increases the difficulty of query.

Still taking the person table as an example, its original data is as follows:

 Incorrect usage 1 :

 The execution result is empty, and no data is queried, as shown in the following figure:

 Incorrect usage 2:

 The execution result is also empty, and no data is queried, as shown in the following figure:

 Correct usage 1 :

 The execution results are as follows:

 

Correct usage 2 :

 The execution results are as follows:

 

Recommended usage

Alibaba's " Java Development Manual" recommends that we use ISNULL (column) to judge NULL values . The reason is that in SQL statements, if a newline is placed before null , it will affect readability; ISNULL (column) is a whole, concise and easy to understand. It is also faster to analyze the execution efficiency of ISNULL (column) from the performance data .

Summarize

In this article, we have talked about 5 problems that may be caused when a column is NULL : loss of query results, null pointer exceptions, and increased query difficulty. Therefore, at the end, it is recommended that you try to set the is not null constraint when creating a table . If a column does not have a value, you can set a null value ( '' ) or 0 as its default value.

-END-

Guess you like

Origin blog.csdn.net/Rocky006/article/details/130489878