Mysql index null

Source: Internet
Author: User
This is the case: the user table contains the usernamechar (16) NOTNULLCOMMENT & #039; username & #039;, passwordchar (32) NOTNULLCOMMENT & #039; password & #039;, emailchar (32) NOTNULLCOMMENT & #039; user email & #039;, mobilechar (15) NOTNULLCOMMENT... the situation is as follows:

These fields are in the User table.
usernameChar (16) not null comment 'username ',
passwordChar (32) not null comment 'Password ',
emailChar (32) not null comment 'user mailboxes ',
mobileChar (15) not null comment 'user phone ',

Username email mobile is unique, so I want to create a unique index for them.

However, during registration, the user may only need a mobile phone number. You do not need to enter the email address or user name. In this case, a problem occurs. If you only have a mobile phone number while the user name is used for registration, the mailbox has a null string by default, but it cannot be inserted because it has a unique index. I want to set null by default, that is, to allow null, but I can see this sentence:

Avoid NULL as much as possible: the column should be specified as not null unless you want to store NULL. In MySQL, it is difficult to query and optimize columns with null values, because they make the index and index statistics and comparison operations more complex. You should replace null values with 0, a special value, or an empty string.

If I have saved null in the user name and mailbox, then there will be no index. Will this field be unable to be indexed? Performance problems?

Please tell me how to solve this problem

Reply content:

The situation is as follows:

These fields are in the User table.
usernameChar (16) not null comment 'username ',
passwordChar (32) not null comment 'Password ',
emailChar (32) not null comment 'user mailboxes ',
mobileChar (15) not null comment 'user phone ',

Username email mobile is unique, so I want to create a unique index for them.

However, during registration, the user may only need a mobile phone number. You do not need to enter the email address or user name. In this case, a problem occurs. If you only have a mobile phone number while the user name is used for registration, the mailbox has a null string by default, but it cannot be inserted because it has a unique index. I want to set null by default, that is, to allow null, but I can see this sentence:

Avoid NULL as much as possible: the column should be specified as not null unless you want to store NULL. In MySQL, it is difficult to query and optimize columns with null values, because they make the index and index statistics and comparison operations more complex. You should replace null values with 0, a special value, or an empty string.

If I have saved null in the user name and mailbox, then there will be no index. Will this field be unable to be indexed? Performance problems?

Please tell me how to solve this problem

The post I saw on the v site may help you solve your problem.
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.