MySQL field content is case sensitive

Source: Internet
Author: User

MySQL field content is case sensitive

Data is migrated from Oracle to MySQL. Since Oracle is case-sensitive in the past, the default configuration is used for MySQL configuration, resulting in some data import failures, and some unique keys report errors and conflicts.

Record the test procedure below.

Database Version: MySQL 5.7.11

Proofreading rules generally have these features:

  • Two different character sets cannot have the same verification rules.
  • Each character set has a default verification rule. For example, the default verification rule for utf8 is utf8_general_ci.
  • There is a naming convention for proofreading rules: they start with their relevant Character Set names, usually include a language name, and are case-insensitive (_ ci), _ cs (case-sensitive) or _ bin (Binary) ends.

View the supported verification rules:

mysql> SHOW COLLATION  like  'utf8%' ; + --------------------------+---------+-----+---------+----------+---------+ | Collation                | Charset | Id  |  Default  | Compiled | Sortlen | + --------------------------+---------+-----+---------+----------+---------+ | utf8_general_ci          | utf8    |  33 | Yes     | Yes      |       1 | | utf8_bin                 | utf8    |  83 |         | Yes      |       1 | | utf8_unicode_ci          | utf8    | 192 |         | Yes      |       8 | ... | utf8mb4_general_ci       | utf8mb4 |  45 | Yes     | Yes      |       1 | | utf8mb4_bin              | utf8mb4 |  46 |         | Yes      |       1 | | utf8mb4_unicode_ci       | utf8mb4 | 224 |         | Yes      |       8 | | utf8mb4_icelandic_ci     | utf8mb4 | 225 |         | Yes      |       8 |

View local verification rules:

mysql> show  global  variables  like  '%coll%' ; + ----------------------+--------------------+ | Variable_name        | Value              | + ----------------------+--------------------+ | collation_connection | utf8mb4_unicode_ci | | collation_database   | utf8mb4_unicode_ci | | collation_server     | utf8mb4_unicode_ci | + ----------------------+--------------------+

The encoding used by the database in production is utf8mb4, and the validation rule is utf8mb4_unicode_ci, Which is case insensitive.

To be case sensitive, change the sorting rule to utf8mb4_bin.

Test Results: After the database configuration is modified, the existing tables are not affected. to take effect, you need to modify the sorting rules for specific columns. The priority is probably like this: column> table> database> Server

There are two ways to make the query case sensitive:

The first method is to modify the column-level validation rule to utf8mb4_bin.

T table

CREATE  TABLE  `T` (   ` name varchar (20)  COLLATE  utf8mb4_unicode_ci  DEFAULT  NULL ) ENGINE=InnoDB  DEFAULT  CHARSET=utf8mb4  COLLATE =utf8mb4_unicode_ci mysql>  select  from  T; + ------+ name  | + ------+ | YOU  | | You  | | you  | | you  | | yOU  | + ------+

Table T2: Modify the column checking rule to utf8mb4_bin.

CREATE  TABLE  `T2` (   ` name varchar (20)  CHARACTER  SET  utf8mb4  COLLATE  utf8mb4_bin  DEFAULT  NULL ) ENGINE=InnoDB  DEFAULT  CHARSET=utf8mb4  COLLATE =utf8mb4_unicode_ci mysql>  select  from  T2; + ------+ name  | + ------+ | yOU  | | you  | + ------+

Query:

T: (Case Insensitive)

mysql>  select  from  where  name  'you' ; + ------+ name  | + ------+ | YOU  | | You  | | you  | | you  | | yOU  | + ------+

T2: (case sensitive)

mysql>  select  from  T2  where  name  'you' ; + ------+ name  | + ------+ | you  | + ------+

Method 2: Use the following query statement instead of modifying the configuration and table structure:

T: (Tables not modified)

mysql>  select  from  where  name  binary 'you' ; + ------+ name  | + ------+ | you  | | you  | + ------+

Related Article

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.