Table of Contents
3. Character sets and character sequences supported by MySQL
Home Database Mysql Tutorial Understand MySQL character set settings in 5 minutes

Understand MySQL character set settings in 5 minutes

Jan 11, 2018 pm 01:15 PM
mysql character set set up

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles