JavaScript-database table A field setting default is null good, or "" Good, or is empty string good? Mobile I just want to return to empty

Source: Internet
Author: User
Tags table definition
When a database field is set to NULL, the phone side will return null by default, and if I want to make the phone return empty, I have to make a judgment.
1, if I want to let the mobile phone end is empty, do not judge, table field default should be how to set it?
2, table field set default is null good, or "" Good, or is empty string good? Are they different?

Reply content:

When a database field is set to NULL, the phone side will return null by default, and if I want to make the phone return empty, I have to make a judgment.
1, if I want to let the mobile phone end is empty, do not judge, table field default should be how to set it?
2, table field set default is null good, or "" Good, or is empty string good? Are they different?

1: null value (') is not space-consuming
Null in 2:mysql is actually space-consuming. Official Document Description:
"NULL columns require additional space in the row to record whether their values is null. For MyISAM tables, each of the NULL column takes one bit extra, rounded up to the nearest byte. "
Therefore, the MySQL design table is not recommended to use the default NULL
A string of type can default ' int ' type can default 0
Landlord want to determine whether the field is empty with PHP processing can

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 of all, 3 of the stackoverflow discussion on the issue, in fact, I personally feel that the default value or NULL it is not much difference, which can be, and there is no particular advantage and disadvantage.

And the interface of the mobile phone to return problems, this general situation is through restful the interface to Exchange data, other languages I am not very clear, but in the implementation of the Java return value filter or the default value conversion is very easy . ReferencesetPropertyInclusion

In high-performance MySQL, it says:

Try to avoid null

It is generally preferable to specify the column as not NULL, unless you really need to store a null value, and if the MySQL table definition does not specify a column NOT NULL, the default is to allow null;

If the query contains nullable columns, it is more difficult to optimize for MySQL. Because nullable columns make indexes, index statistics, and value comparisons more complex;

Nullable columns use more storage space, and special handling is required in MySQL.

When a nullable column is indexed, an extra byte is required for each index record, and in MyISAM it can even cause a fixed-size index (for example, an index of only one integer column) to become a variable-size index;

In general, changing a nullable column to not NULL results in a small performance boost, so there is no need to modify this situation first, unless you are sure that this can cause problems;

However, if you plan to index on a column, you should try to avoid designing nullable columns. Of course there are exceptions, such as InnoDB, which uses a separate bit to store null values, and has a good spatial efficiency for sparse data. This does not apply to MyISAM.
(Sparse data: means that many values are null, and the minority values are non-null)

Personally think that the main look at you for the location of the null, the location of the exception should not occur when the recommendation is set to NULL, if the location of the normal possible situation is "better."

  • 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.