Home > Database > Mysql Tutorial > Understand MySQL character set settings in 5 minutes

Understand MySQL character set settings in 5 minutes

小云云
Release: 2018-01-11 13:15:59
Original
1280 people have browsed it

This article mainly introduces the concepts and connections of mysql character set and character order, and shares with you various ways to view the character sets supported by MYSQL. Please refer to this article for specific details, I hope it can help you.

1. Content Overview

In the process of using MySQL, it is very important to understand the concepts of character set and character order, as well as the impact of different settings on data storage and comparison. . The "garbled code" problem that many students encounter in their daily work is most likely caused by an insufficient understanding of character sets and character sequences and incorrect settings.

This article introduces the following contents from the shallower to the deeper:

  1. The basic concepts and connections of character sets and character sequences

  2. Character set and character sequence setting levels supported by MySQL, and the relationship between each setting level

  3. server, database, table, column-level character set and character sequence Check and set

  4. #When should you set the character set and character sequence

2. Character set and character sequence Concepts and connections

In terms of data storage, MySQL provides different character set support. For data comparison operations, different character order support is provided.

MySQL provides different levels of settings, including server level, database level, table level, and column level, which can provide very precise settings.

What is a character set and character sequence? To put it simply:

  1. Character set (character set): defines the characters and character encoding.

  2. Character order (collation): defines the comparison rules of characters.

For example:

has four characters: A, B, a, b. The codes of these four characters are A = 0, B = 1. , a = 2, b = 3. The characters + encoding here constitute a character set.

What if we want to compare the size of two characters? For example, A, B, or a, b, the most intuitive way to compare is to use their encoding, for example, because 0 < 1, so A < B.

In addition, for A and a, although they have different encodings, we feel that uppercase and lowercase characters should be equal, that is to say, A == a.

Two comparison rules are defined above, and the set of these comparison rules is collation.

  1. If they are both uppercase and lowercase characters, compare their encoding sizes;

  2. If the two characters have a case relationship, then they equal.

3. Character sets and character sequences supported by MySQL

MySQL supports multiple character sets and character sequences.

  1. A character set corresponds to at least one character sequence (usually 1 to many).

  2. Two different character sets cannot have the same character sequence.

  3. Each character set has a default character order.

The above is relatively abstract. Let’s look at the next few sections to understand what’s going on.

1. Check the supported character sets

You can check the character sets supported by MYSQL through the following methods.

Method 1:

mysql> SHOW CHARACTER SET;
+----------+-----------------------------+---------------------+--------+
| Charset | Description   | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European  | dec8_swedish_ci | 1 |
...省略</p>
<p>Method 2: </p>
<pre class="brush:php;toolbar:false">mysql> use information_schema;
mysql> select * from CHARACTER_SETS;
+--------------------+----------------------+-----------------------------+--------+
| CHARACTER_SET_NAME | DEFAULT_COLLATE_NAME | DESCRIPTION   | MAXLEN |
+--------------------+----------------------+-----------------------------+--------+
| big5  | big5_chinese_ci | Big5 Traditional Chinese | 2 |
| dec8  | dec8_swedish_ci | DEC West European  | 1 |
...省略
Copy after login

When viewing using SHOW CHARACTER SET, you can also add WHERE or LIKE qualifying conditions.

Example 1: Use WHERE qualification conditions.

mysql> SHOW CHARACTER SET WHERE Charset="utf8";
+---------+---------------+-------------------+--------+
| Charset | Description | Default collation | Maxlen |
+---------+---------------+-------------------+--------+
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
+---------+---------------+-------------------+--------+
1 row in set (0.00 sec)
Copy after login

Example 2: Use LIKE qualification conditions.

mysql> SHOW CHARACTER SET LIKE "utf8%";
+---------+---------------+--------------------+--------+
| Charset | Description | Default collation | Maxlen |
+---------+---------------+--------------------+--------+
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
+---------+---------------+--------------------+--------+
2 rows in set (0.00 sec)
Copy after login

2. Check the supported character sequence

Similarly, you can check the character sequence supported by MYSQL in the following way.

Method 1: Check through SHOW COLLATION.

As you can see, the utf8 character set has more than 10 character sequences. Determine whether the character sequence is the default by whether the value of Default is Yes.

mysql> SHOW COLLATION WHERE Charset = 'utf8';
+--------------------------+---------+-----+---------+----------+---------+
| Collation  | Charset | Id | Default | Compiled | Sortlen |
+--------------------------+---------+-----+---------+----------+---------+
| utf8_general_ci  | utf8 | 33 | Yes | Yes | 1 |
| utf8_bin   | utf8 | 83 |  | Yes | 1 |
...略
Copy after login

Method 2: Query information_schema.COLLATIONS.

mysql> USE information_schema;
mysql> SELECT * FROM COLLATIONS WHERE CHARACTER_SET_NAME="utf8";
+--------------------------+--------------------+-----+------------+-------------+---------+
| COLLATION_NAME  | CHARACTER_SET_NAME | ID | IS_DEFAULT | IS_COMPILED | SORTLEN |
+--------------------------+--------------------+-----+------------+-------------+---------+
| utf8_general_ci  | utf8  | 33 | Yes | Yes  | 1 |
| utf8_bin   | utf8  | 83 |  | Yes  | 1 |
| utf8_unicode_ci  | utf8  | 192 |  | Yes  | 8 |
Copy after login

3. Naming specification of character sequence

The naming of character sequence is prefixed by its corresponding character set, as shown below. For example, the character sequence utf8_general_ci indicates that it is the character sequence of the character set utf8.

For more rules, please refer to the official documentation.

MariaDB [information_schema]> SELECT CHARACTER_SET_NAME, COLLATION_NAME FROM COLLATIONS WHERE CHARACTER_SET_NAME="utf8" limit 2; 
+--------------------+-----------------+
| CHARACTER_SET_NAME | COLLATION_NAME |
+--------------------+-----------------+
| utf8  | utf8_general_ci |
| utf8  | utf8_bin |
+--------------------+-----------------+
2 rows in set (0.00 sec)
Copy after login

4. Server character set and character sequence

Purpose: When you create a database and do not specify the character set and character sequence, the server character set and server character sequence will Will be used as the default character set and collation of the database.

How to specify: When the MySQL service is started, it can be specified through command line parameters. It can also be specified through variables in the configuration file.

Server default character set and character sequence: specified through compilation parameters when MySQL is compiled.

character_set_server and collation_server correspond to the server character set and server character sequence respectively.

1. Check the server character set and character sequence.

corresponds to the two system variables character_set_server and collation_server respectively.

mysql> SHOW VARIABLES LIKE "character_set_server";
mysql> SHOW VARIABLES LIKE "collation_server";
Copy after login

2. Specify

when starting the service. You can specify the server character set and character sequence when starting the MySQL service. If not specified, the default character sequences are latin1, latin1_swedish_ci

mysqld --character-set-server=latin1 \
 --collation-server=latin1_swedish_ci
Copy after login

Specify the server character set separately. At this time, the server character sequence is latin1's default character sequence latin1_swedish_ci.

mysqld --character-set-server=latin1
Copy after login

3. Configuration file specification

In addition to specifying in the command line parameters, it can also be specified in the configuration file, as shown below.

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
Copy after login

4. Runtime modification

Example: Runtime modification (it will become invalid after restarting. If you want it to remain unchanged after restarting, you need to write it into the configuration file)

mysql> SET character_set_server = utf8 ;
Copy after login

5、编译时指定默认字符集、字符序

character_set_server、collation_server的默认值,可以在MySQL编译时,通过编译选项指定:

cmake . -DDEFAULT_CHARSET=latin1 \
  -DDEFAULT_COLLATION=latin1_german1_ci
Copy after login

五、database的字符集、字符序

用途:指定数据库级别的字符集、字符序。同一个MySQL服务下的数据库,可以分别指定不同的字符集/字符序。

1、设置数据的字符集/字符序

可以在创建、修改数据库的时候,通过CHARACTER SET、COLLATE指定数据库的字符集、排序规则。

创建数据库:

CREATE DATABASE db_name
 [[DEFAULT] CHARACTER SET charset_name]
 [[DEFAULT] COLLATE collation_name]
Copy after login

修改数据库:

ALTER DATABASE db_name
 [[DEFAULT] CHARACTER SET charset_name]
 [[DEFAULT] COLLATE collation_name]
Copy after login

例子:创建数据库test_schema,字符集设置为utf8,此时默认的排序规则为utf8_general_ci。

CREATE DATABASE `test_schema` DEFAULT CHARACTER SET utf8;
Copy after login

2、查看数据库的字符集/字符序

有3种方式可以查看数据库的字符集/字符序。

例子一:查看test_schema的字符集、排序规则。(需要切换默认数据库)

mysql> use test_schema;
Database changed
mysql> SELECT @@character_set_database, @@collation_database;
+--------------------------+----------------------+
| @@character_set_database | @@collation_database |
+--------------------------+----------------------+
| utf8   | utf8_general_ci |
+--------------------------+----------------------+
1 row in set (0.00 sec)
Copy after login

例子二:也可以通过下面命令查看test_schema的字符集、数据库(不需要切换默认数据库)

mysql> SELECT SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE schema_name="test_schema";
+-------------+----------------------------+------------------------+
| SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME |
+-------------+----------------------------+------------------------+
| test_schema | utf8   | utf8_general_ci |
+-------------+----------------------------+------------------------+
1 row in set (0.00 sec)
Copy after login

例子三:也可以通过查看创建数据库的语句,来查看字符集。

mysql> SHOW CREATE DATABASE test_schema;
+-------------+----------------------------------------------------------------------+
| Database | Create Database       |
+-------------+----------------------------------------------------------------------+
| test_schema | CREATE DATABASE `test_schema` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+-------------+----------------------------------------------------------------------+
1 row in set (0.00 sec)
Copy after login

3、database字符集、字符序是怎么确定的

创建数据库时,指定了CHARACTER SET或COLLATE,则以对应的字符集、排序规则为准。
创建数据库时,如果没有指定字符集、排序规则,则以character_set_server、collation_server为准。

六、table的字符集、字符序

创建表、修改表的语法如下,可通过CHARACTER SET、COLLATE设置字符集、字符序。

CREATE TABLE tbl_name (column_list)
 [[DEFAULT] CHARACTER SET charset_name]
 [COLLATE collation_name]]

ALTER TABLE tbl_name
 [[DEFAULT] CHARACTER SET charset_name]
 [COLLATE collation_name]
Copy after login

1、创建table并指定字符集/字符序

例子如下,指定字符集为utf8,字符序则采用默认的。

CREATE TABLE `test_schema`.`test_table` (
 `id` INT NOT NULL COMMENT '',
 PRIMARY KEY (`id`) COMMENT '')
DEFAULT CHARACTER SET = utf8;
Copy after login

2、查看table的字符集/字符序

同样,有3种方式可以查看table的字符集/字符序。

方式一:通过SHOW TABLE STATUS查看table状态,注意Collation为utf8_general_ci,对应的字符集为utf8。

MariaDB [blog]> SHOW TABLE STATUS FROM test_schema \G;
*************************** 1. row ***************************
  Name: test_table
  Engine: InnoDB
 Version: 10
 Row_format: Compact
  Rows: 0
 Avg_row_length: 0
 Data_length: 16384
Max_data_length: 0
 Index_length: 0
 Data_free: 11534336
 Auto_increment: NULL
 Create_time: 2018-01-09 16:10:42
 Update_time: NULL
 Check_time: NULL
 Collation: utf8_general_ci
 Checksum: NULL
 Create_options: 
 Comment: 
1 row in set (0.00 sec)
Copy after login

方式二:查看information_schema.TABLES的信息。

mysql> USE test_schema;
mysql> SELECT TABLE_COLLATION FROM information_schema.TABLES WHERE TABLE_SCHEMA = "test_schema" AND TABLE_NAME = "test_table";
+-----------------+
| TABLE_COLLATION |
+-----------------+
| utf8_general_ci |
+-----------------+
Copy after login

方式三:通过SHOW CREATE TABLE确认。

mysql> SHOW CREATE TABLE test_table;
+------------+----------------------------------------------------------------------------------------------------------------+
| Table | Create Table             |
+------------+----------------------------------------------------------------------------------------------------------------+
| test_table | CREATE TABLE `test_table` (
 `id` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+------------+----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Copy after login

3、table字符集、字符序如何确定

假设CHARACTER SET、COLLATE的值分别是charset_name、collation_name。如果创建table时:

明确了charset_name、collation_name,则采用charset_name、collation_name。
只明确了charset_name,但collation_name未明确,则字符集采用charset_name,字符序采用charset_name对应的默认字符序。

只明确了collation_name,但charset_name未明确,则字符序采用collation_name,字符集采用collation_name关联的字符集。

charset_name、collation_name均未明确,则采用数据库的字符集、字符序设置。

七、column的字符集、排序

类型为CHAR、VARCHAR、TEXT的列,可以指定字符集/字符序,语法如下:

col_name {CHAR | VARCHAR | TEXT} (col_length)
 [CHARACTER SET charset_name]
 [COLLATE collation_name]
Copy after login

1、新增column并指定字符集/排序规则

例子如下:(创建table类似)

mysql> ALTER TABLE test_table ADD COLUMN char_column VARCHAR(25) CHARACTER SET utf8;
Copy after login

2、查看column的字符集/字符序

例子如下:

mysql> SELECT CHARACTER_SET_NAME, COLLATION_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA="test_schema" AND TABLE_NAME="test_table" AND COLUMN_NAME="char_column";
+--------------------+-----------------+
| CHARACTER_SET_NAME | COLLATION_NAME |
+--------------------+-----------------+
| utf8  | utf8_general_ci |
+--------------------+-----------------+
1 row in set (0.00 sec)
Copy after login

3、column字符集/排序规则确定

假设CHARACTER SET、COLLATE的值分别是charset_name、collation_name:

如果charset_name、collation_name均明确,则字符集、字符序以charset_name、collation_name为准。

只明确了charset_name,collation_name未明确,则字符集为charset_name,字符序为charset_name的默认字符序。

只明确了collation_name,charset_name未明确,则字符序为collation_name,字符集为collation_name关联的字符集。

charset_name、collation_name均未明确,则以table的字符集、字符序为准。

八、选择:何时设置字符集、字符序

一般来说,可以在三个地方进行配置:

创建数据库的时候进行配置。

mysql server启动的时候进行配置。

从源码编译mysql的时候,通过编译参数进行配置

1、方式一:创建数据库的时候进行配置

这种方式比较灵活,也比较保险,它不依赖于默认的字符集/字符序。当你创建数据库的时候指定字符集/字符序,后续创建table、column的时候,如果不特殊指定,会继承对应数据库的字符集/字符序。

CREATE DATABASE mydb
 DEFAULT CHARACTER SET utf8
 DEFAULT COLLATE utf8_general_ci;
Copy after login

2、方式二:mysql server启动的时候进行配置

可以添加以下配置,这样mysql server启动的时候,会对character-set-server、collation-server进行配置。

当你通过mysql client创建database/table/column,且没有显示声明字符集/字符序,那么就会用character-set-server/collation-server作为默认的字符集/字符序。

另外,client、server连接时的字符集/字符序,还是需要通过SET NAMES进行设置。

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
Copy after login

3、方式三:从源码编译mysql的时候,通过编译参数进行设置

编译的时候如果指定了-DDEFAULT_CHARSET和-DDEFAULT_COLLATION,那么:

创建database、table时,会将其作为默认的字符集/字符序。

client连接server时,会将其作为默认的字符集/字符序。(不用单独SET NAMES)

shell> cmake . -DDEFAULT_CHARSET=utf8 \
  -DDEFAULT_COLLATION=utf8_general_ci
Copy after login

九、写在后面

本文较为详细地介绍了MySQL中字符集、字符序相关的内容,这部分内容主要针对的是数据的存储与比较。其实还有很重要的一部分内容还没涉及:针对连接的字符集、字符序设置。

由于连接的字符集、字符序设置不当导致的乱码问题也非常多,这部分内容展开来讲内容也不少,放在下一篇文章进行讲解。

相关推荐:

MySQL字符集和校对顺序简介

关于MySQL字符集问题详解(图)

mysql字符集和校对规则(Mysql校对集)

The above is the detailed content of Understand MySQL character set settings in 5 minutes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template