Pitfalls based on MySQL default sorting rules

Pitfalls based on MySQL default sorting rules

The default varchar type in MySQL is case insensitive (insensitive). If you want MySQL to be case sensitive, you need to set the sorting rules:

utf8_bin stores each character in a string as binary data, distinguishing between uppercase and lowercase letters.

utf8_genera_ci is case-insensitive. ci is the abbreviation of case insensitive, which means it is case-insensitive.

utf8_general_cs is case-sensitive. cs is the abbreviation of case sensitive.

Using utf8_genera_ci is not case-sensitive, which causes problems when the content of this field is case-sensitive, such as when it is used as a case-sensitive code or verification code.

The option utf8_general_cs is generally not available, so you can only use utf8_bin to distinguish between upper and lower cases.

1) Setting the sorting rules is reversible. If the previously set sorting rules do not meet the requirements, garbled characters may appear after changing the sorting rules. When the original sorting rules are restored, the garbled characters will disappear.

2) You can change the varchar type to varbinary

3) If you have already used the default sorting rule, utf8_genera_ci, and want to query the results case-sensitively, you can restrict it when querying:

select binary column from table;

or

select column2 from table where binary cloumn;

Supplement: Character set and collation in MySQL

Character Set:

Character is the general term for various texts and symbols, and character set is a collection of characters. Common character set names: ASCII character set, GB2312 character set, BIG5 character set, GB18030 character set, Unicode character set, etc.

Character encoding is required for computers to recognize text in various character sets. So some character sets use 2 bytes per character, and some character sets use 3 bytes per character.

Sorting rules: Examples

What is the difference between utf_bin and utf_general_ci?

ci stands for case insensitive, which means "case insensitive". a and A will be treated the same in character judgment;

bin is binary, a and A will be treated differently.

utf8_general_ci is case-insensitive, which you should use when registering your username and email address.

utf8_general_cs is case-sensitive. If you use this for your username and email address, it will cause adverse consequences.

utf8_bin: string Each string is stored as binary data. It is case-sensitive and can store binary content.

Therefore, the sorting rules mainly describe whether it is case-sensitive and the storage method.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Example of utf8mb4 collation in MySQL
  • MySQL aggregate function sorting
  • MySQL sorting using index scan
  • Some lesser-known sorting methods in MySQL
  • Mysql Chinese sorting rules description
  • MySQL sorting principles and case analysis
  • MySQL query sorting and paging related
  • How to use indexes to optimize MySQL ORDER BY statements
  • Mysql sorting and paging (order by & limit) and existing pitfalls
  • MySQL sorting feature details

<<:  Simple operation of installing vi command in docker container

>>:  Simple example of adding and removing HTML nodes

Recommend

Detailed explanation of Angular dynamic components

Table of contents Usage scenarios How to achieve ...

5 tips for writing CSS to make your style more standardized

1. Arrange CSS in alphabetical order Not in alphab...

Tutorial diagram of installing zabbix2.4 under centos6.5

The fixed IP address of the centos-DVD1 version s...

Super detailed basic JavaScript syntax rules

Table of contents 01 JavaScript (abbreviated as: ...

A brief analysis of the knowledge points of exporting and importing MySQL data

Often, we may need to export local database data ...

Implementation of Vue 3.x project based on Vite2.x

Creating a Vue 3.x Project npm init @vitejs/app m...

The difference between HTML iframe and frameset_PowerNode Java Academy

Introduction 1.<iframe> tag: iframe is an i...

How to create a simple column chart using Flex layout in css

The following is a bar chart using Flex layout: H...

How to use Node.js to determine whether a png image has transparent pixels

background PNG images take up more storage space ...