Javascript-is it better to set a field in the database table to Null by default, or "", or EmptyString? On the mobile phone, I just want to return null

Source: Internet
Author: User
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

ThreestackoverflowLet's talk about this. In fact, I personally think it's the default value orNULLThere is no big difference between them. there is no special advantage or disadvantage.

An error is returned from the mobile phone interface.restfulInterface to exchange data, I am not very clear about other languages, but inJavaTo 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.