MySQL index null problem

Source: Internet
Author: User
This is the case:

There are several fields in the user table
usernamechar (+) not NULL COMMENT ' user name ',
passwordchar (+) not NULL COMMENT ' password ',
emailchar (+) not NULL COMMENT ' user mailbox ',
mobilechar (+) not NULL COMMENT ' user phone ',

Username Email Mobile is unique, so I want to create a unique index on them

But when registering, the user may only need the mobile phone number to be able, does not have to fill in the mailbox and the user name, at this time has the question, if registers only the mobile phone number, but the user name, the mailbox default empty string, but because has the unique index, therefore cannot insert, I want to set the default null, is allowed to null But then I saw this sentence:

Try to avoid null: The column should be specified as NOT NULL unless you want to store null. In MySQL, columns with null values are difficult to query optimization because they complicate indexing, index statistics, and comparison operations. You should use 0, a special value, or an empty string instead of a null value.

If I have null in my username and mailbox, I don't have an index, can this field be indexed? Have a performance problem?

Ask the great God to tell me how to solve this problem.

Reply content:

This is the case:

There are several fields in the user table
usernamechar (+) not NULL COMMENT ' user name ',
passwordchar (+) not NULL COMMENT ' password ',
emailchar (+) not NULL COMMENT ' user mailbox ',
mobilechar (+) not NULL COMMENT ' user phone ',

Username Email Mobile is unique, so I want to create a unique index on them

But when registering, the user may only need the mobile phone number to be able, does not have to fill in the mailbox and the user name, at this time has the question, if registers only the mobile phone number, but the user name, the mailbox default empty string, but because has the unique index, therefore cannot insert, I want to set the default null, is allowed to null But then I saw this sentence:

Try to avoid null: The column should be specified as NOT NULL unless you want to store null. In MySQL, columns with null values are difficult to query optimization because they complicate indexing, index statistics, and comparison operations. You should use 0, a special value, or an empty string instead of a null value.

If I have null in my username and mailbox, I don't have an index, can this field be indexed? Have a performance problem?

Ask the great God to tell me how to solve this problem.

The posts you see on the V-station may help you with your doubts.
https://www.v2ex.com/t/60437

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