Why is it recommended that mysql has a default value instead of null?

1.Official original text

First, before understanding this problem, let’s read the original text of mysql official document:
Insert image description here

NULL columns require additional space in the row to record whether their values ​​are NULL. Each NULL column takes one bit extra, rounded up to the nearest byte. Let me translate: NULL columns require additional space in the row to record whether their values ​​are
NULL
. NULL. Each empty column requires an extra bit, rounded to the nearest byte

Many people say that null does not take up space in the database, which means that this answer is naturally broken. The official said that null columns require extra space to record. Naturally, if space is needed, we can also
pass Test to see the specific effect:
Insert image description here
From this SQL, we can clearly see that the length of the empty string is 0, which does not occupy space; 1 occupies a length, and the length of null is null, as above The figure says null requires extra space in the line to record whether they are null, but in fact it takes up space.

Not only that, null and most calculations will not produce results. They will only produce a null. The only
Insert image description here
Insert image description here
keywords that can be linked to null are the few keywords given by mysql, is null, is not null, <=>, and even The commonly used = has nothing to do with null (mysql returns 1 for true and 0 for false)
Insert image description here
Insert image description here
Insert image description here
and even affects the final result in some function calculations:
Insert image description here

Insert image description here
Although the count (field) function is not commonly used by us, it can be clearly seen that it has an impact on the results in the calculation. The number of count (field) is definitely less than or equal to count (*)

Borrowing a sentence from Zhihu@gekexiaojun:
In a popular sense: ('') A string null value is like a vacuum transition cup, with nothing in it, while a NULL value is a cup filled with air, although They all look the same, but there are essential differences

2. Hidden dangers of setting to null

In MySQL, there are several main reasons why NULL fields are not recommended:

  1. Data consistency: NULL values ​​may introduce data inconsistency. When a field is allowed to be NULL, it can contain no value, which can lead to unexpected results when querying and processing the data. For example, using NULL values ​​for mathematical calculations can lead to unexpected results.

  2. Query complexity: Using NULL values ​​may increase query complexity. When querying data, you need to handle special cases of NULL values, such as operation failures caused by null values ​​in some fields, which may increase the complexity of query logic and code.

  3. Storage space: NULL values ​​require additional storage space. For each field that allows NULL, MySQL needs to reserve additional space for the field to represent NULL values, which increases the storage requirements of the data table.

  4. Index efficiency: NULL values ​​may reduce index efficiency. When a field is allowed to be NULL, the index on that field may become larger and sparser, which will have a certain impact on query performance. In MySQL, you can use indexes when using IS NULL query conditions. But some conditions need to be met before it can be used.
    4.1 NULL values ​​are rare. When there are many NULL values, MySQL may choose to abandon using the index and perform a full table scan, because for most rows, a full table scan is faster than using an index.
    4.2 and there are no duplicate values ​​in the index column. If there are duplicate NULL values ​​in an indexed column, MySQL may abandon using the index because multiple index entries need to be scanned to find NULL values ​​in the index.
    4.3 Use the equals operator instead of IS NULL. If you query with =NULL, MySQL may not use the index because the equals operator does not work for NULL values.

3. Summary

When designing the database schema, if a field is expected to store values, you can use default values ​​or non-null constraints to avoid the generation of NULL values. Using default values ​​ensures that a field always contains a valid value, and a non-null constraint enforces that the field is not allowed to be null. This simplifies query logic and improves database performance and reliability. If a field really needs to store unknown or missing values, consider using a special placeholder value instead of NULL, such as an empty string ("") or a specific identification value.

Guess you like

Origin blog.csdn.net/weixin_52796198/article/details/131538878