MySQL character sets and comparison rules
Since the need to transmit the encoded string communication between client and server MySQL, it will inevitably produce transcoding
character set
MySQL, utf8
is utf8mb3
, only 1 to 3 bytes represent characters
utf-8md4
using 1 to 4 bytes characters
Related parameters
MySQL server has the following parameters related to the coded
character set parameters:
character_set_server
server-level character set of
character_set_database
the current character set of the database (if no USE select the database, the server-level view of the character set) read-only, can not be changed by modifying the current database parameter
character set conversion parameters:
character_set_client
the server that the client sends a request for the character set, the request is decoded to form the character set, content acquisition request for
character_set_connection
the character set used when the internal server processes the request, the server will be the client request is encoded in the form of the character set (if the character set of the data processing will be different then a conversion)
character_set_results
the results returned by the server will be encoded in the form of the character set returned to the client
View command
MySQL supports many character sets, you can view all character sets supported by the following command
SHOW CHARACTER SET;
SHOW CHARSET;
Which Default collation
is the default character set of this comparison rule, Maxlen
is this character set maximum number of bytes needed a character
View other parameters
SHOW VARIABLES LIKE 'character_set_server';
SHOW VARIABLES LIKE 'character_set_database';
SHOW VARIABLES LIKE 'character_set_client';
SHOW VARIABLES LIKE 'character_set_connection';
SHOW VARIABLES LIKE 'character_set_results';
Set command
Character Set Parameters
Server level:
SET character_set_server 字符集名;
Database level:
CREATE DATABASE 数据库名
CHARACTER SET 字符集名称;
ALTER DATABASE 数据库名
CHARACTER SET 字符集名称;
Table level
CREATE TABLE 表名 (列的信息)
CHARACTER SET 字符集名称;
ALTER TABLE 表名
CHARACTER SET 字符集名称;
Column level
CREATE TABLE 表名(列名 字符串类型 CHARACTER SET 字符集名称);
ALTER TABLE 表名 MODIFY 列名 字符串类型 CHARACTER SET 字符集名称 ;
Set the character set conversion parameters
In order to facilitate the general three parameters will be set to the same character set
, use the following command can be set three parameters simultaneously
SET NAMES 字符集名;
Of course, you can set separately
SET character_set_client = 字符集名;
SET character_set_connection = 字符集名;
SET character_set_results = 字符集名;
Comparison rules
Comparative collation rules is sometimes referred to, for comparing the size of the character string predetermined in the order of
the same characters may have various comparison rule
Naming Rules
Each character set may correspond to a variety of comparison rules, these rules naming rules to compare
- Comparison rules names that begin with the corresponding character set name
- Mainly for the middle portion represents what language
- Suffixes are the following:
`_ai` | accent insensitive | No accent |
`_as` | accent sensitive | Accent |
`_ci` | case insensitive | not case sensitive |
`_cs` | case sensitive | Case sensitive |
`_bin` | binary | Compare binary mode |
Example:
utf8_spanish_ci
expressed in Spanish comparison is not case sensitive
Related parameters
collation_server
Server-level comparison rules
collation_database
comparison of the current rules of the database (if no USE select the database, look at the server level comparison rules) read-only, can not be changed by modifying the parameters of the current database
View command
The comparison rules list them all more, you can filter based on the corresponding character set
SHOW COLLATION;
SHOW COLLATION LIKE 'utf8\_%';
Which Charset
indicates that the corresponding character set, Default
indicates whether the default comparison rules
Set command
Set more rule parameters
Server level:
SET collation_server 比较规则名;
Database level:
CREATE DATABASE 数据库名
COLLATE 比较规则名称;
ALTER DATABASE 数据库名
COLLATE 比较规则名称;
Table level
CREATE TABLE 表名 (列的信息)
COLLATE 比较规则名称;
ALTER TABLE 表名
COLLATE 比较规则名称;
Column level
CREATE TABLE 表名(列名 字符串类型 COLLATE 比较规则名称);
ALTER TABLE 表名 MODIFY 列名 字符串类型 COLLATE 比较规则名称 ;
to sum up
When only modify the character set or a level of comparison rules, the same rules or the level of the corresponding comparator will also change the character set to the corresponding (default)
when creating a database if you do not specify the character set and comparison rules, the default use server-level parameter
does not specify the character set and comparison rules when creating a table, the default parameters using the database where the table
is created does not specify the character set and comparison rules when a column, use the column where the table of default parameters