When a field in the database is set to Null, the mobile phone will return Null by default. if I want the mobile phone to return Null, I have to make a judgment. 1. if I want to leave the returned result blank on the mobile phone, I will not make any judgment. how should I set the table field default? 2. set the default value of the table field to Null, or... when a field in the database is set to Null, the mobile phone will return Null by default. if I want the mobile phone to return Null, I have to make a judgment.
1. if I want to leave the returned result blank on the mobile phone, I will not make any judgment. how should I set the table field default?
2. is it better to set the default value for a table field to Null, "", or "Empty String? What are their differences?
Reply content:
When a field in the database is set to Null, the mobile phone will return Null by default. if I want the mobile phone to return Null, I have to make a judgment.
1. if I want to leave the returned result blank on the mobile phone, I will not make any judgment. how should I set the table field default?
2. is it better to set the default value for a table field to Null, "", or "Empty String? What are their differences?
1: NULL value ('') does not occupy space
2: NULL in MySQL actually occupies space. Official documentation:
"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 ."
Therefore, we recommend that you do not use default NULL when designing a mysql table.
If the string type is 'default', the int type can be 'default 0 '.
The landlord wants to determine whether the field is empty. 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
Threestackoverflow
Let's talk about this. In fact, I personally think it's the default value orNULL
There is no big difference between them. there is no special advantage or disadvantage.
An error is returned from the mobile phone interface.restful
Interface to exchange data, I am not very clear about other languages, but inJava
To filter the returned values or convert the default valueseasy
. ReferencesetPropertyInclusion
This is what high-performance mysql says:
Avoid NULL
Generally, it is best to specify the column not null unless the NULL value needs to be stored. when defining a mysql table, if the column not null is NOT specified, NULL is allowed by default;
If the query contains columns that can be NULL, it is more difficult for mysql to optimize. A column that can be NULL makes the index, index statistics, and value comparison more complex;
Columns that can be NULL use more storage space, and special processing is also required in MYSQL.
When a column that can be NULL is indexed, each index record requires an additional byte, which may even lead to a fixed size index in MyISAM (for example, an index with only one integer column) changes to a variable-size index;
Generally, changing a column that can be NULL to not null results in a relatively small performance improvement. Therefore, you do NOT need to modify this situation first when tuning, unless you are sure that this will cause problems;
However, if you plan to create an index on a column, try to avoid designing a column that can be NULL. Of course, there are also exceptions. for example, InnoDB uses a separate bit to store NULL values, which provides a good space efficiency for sparse data. This does not apply to MyISAM.
(Sparse data: many values are NULL, and a few values are non-NULL)
I personally think that it mainly depends on your location for null, and it is recommended that you set it to null if it is positioned as an exception or error, if the location is normal, it is better to set it.