When a certain field in the database is set to Null, the mobile terminal will return Null by default. If I want the mobile terminal to return null, I have to make a judgment.
1. If I want the mobile phone to return empty without making any judgment, how should I set the table field default?
2. Is it better to set the table field default to Null, "", or Empty String? Are they different?
When a certain field in the database is set to Null, the mobile terminal will return Null by default. If I want the mobile terminal to return null, I have to make a judgment.
1. If I want the mobile phone to return empty without making any judgment, how should I set the table field default?
2. Is it better to set the table field default to Null, "", or Empty String? Are they different?
1: NULL value ('') does not take up space
2: NULL in MySQL actually takes up space. Official document description:
"NULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte."
So it is not recommended when designing tables in MySQL Use default NULL
string type can be default '' int type can be default 0
If the poster wants to determine whether the field is empty, just use php to process it
SQL: Using NULL values vs. default values
Is it good to use default: NULL?
No default value Vs NULL Vs 0 in MySQL as a default value for text and integer fields
First, let me share with you 3 discussions on this issue on stackoverflow
. In fact, I personally feel that there is no big difference between using the default value or NULL
. Either one is fine and there is no special advantage. Disadvantages.
The interface return problem with the mobile phone. Generally, data is exchanged through the restful
interface. I am not very clear about other languages, but it is very easy
to implement return value filtering or default value conversion in Java
. ReferencesetPropertyInclusion
This is what is said in "High Performance MySQL":
Try to avoid NULL
Normally, it is best to specify the column as NOT NULL, unless you really need to store NULL values; if the column is not specified as NOT NULL when the mysql table is defined, NULL is allowed by default;
It is more difficult for MySQL to optimize if the query contains columns that can be NULL. Because columns that can be NULL make indexes, index statistics, and value comparisons more complicated;
Columns that can be NULL will use more storage space and require special processing in MYSQL.
When a NULL-able column is indexed, each index record requires an extra byte, which may even cause a fixed-size index (such as an index with only one integer column) to become a variable-size index in MyISAM;
Usually, the performance improvement brought by changing a NULL-able column to NOT NULL is relatively small, so there is no need to modify this situation first when tuning, unless it is determined that this will cause problems;
But if you plan to build an index on the column, you should try to avoid designing the column to be NULL. Of course, there are exceptions. For example, InnoDB uses a separate bit to store NULL values, which has good space efficiency for sparse data. This does not apply to MyISAM.
(Sparse data: refers to many values are NULL, and a few values are non-NULL)
Personally, I think it mainly depends on your positioning of null. If the positioning is an abnormal error that should not occur, it is recommended to set it to null. If the positioning is a normal and possible situation, it is better to set it to ''.