当前位置:网站首页>[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle
2022-06-24 08:51:00 【Man Nong Feige】
Hello! , I'm Manon Feige , Thank you for reading this article , Welcome to three links with one button .
1. Python Basic column , Basic knowledge in a net ,9.9 Yuan can't buy a loss , I can't buy it . Python From entry to mastery
️ 2. Python Crawler column , Systematically learn the knowledge points of reptiles .9.9 Yuan can't buy a loss , I can't buy it .python Reptile beginner level
️ 3. Ceph actual combat , Everything from principle to actual combat . Ceph actual combat
️ 4. Java Introduction to high concurrency programming , Punch in to learn Java High concurrency . Java Introduction to high concurrency programming
5. Take a stroll around the community , Weekly benefits , There are surprises every week . Manon Feige community , Leap plan
The whole network has the same name 【 Manon Feige 】 Welcome to your attention , personal VX: wei158556
List of articles
1. brief introduction
It's officially starting today MySQL Learning from , Skip the basics first , Go directly to the advanced part of the study . Main reference B In the station MySQL Database tutorial ,mysql The installation to mysql senior , strong ! hard !
2. Environmental Science
| Environmental Science | edition |
|---|---|
| Red Hat | 4.8.5-39 |
| MySQL | 5.7 |
3. Character set
3.1. Modify character set
The first thing to be introduced is the character set ,MySQL 5.7 The default character set is latin1, stay MySQL 8.0 The default character set in the future is utf8mb4.
latin1 The problem with character sets is that they cannot save Chinese , Therefore, you need to modify the server's default character set , It just needs to be modified /etc/my.cnf file , Through the command vim /etc/my.cnf Open profile , Add the following configuration to the file :
character-set-server=utf8

Restart after adding the configuration MySQL The server .
systemctl restart mysqld.service
3.2. View the system character set
First we need to pass mysql -u[ user name ] -p[ password ] ( for example :mysql -uroot -pmysql) Enter into mysql Command line , Then you can view the following commands MySQL The character set encoding of the server :
show variables like '%character%';

among :
- character_set_client: The character set used by the server to decode the request
- character_set_connection: When the server processes the request, it will change the request string from character_set_client turn character_set_connection
- character_set_database: The character set of the current database
- character_set_server: Server level character set
- character_set_results: The character set used by the server to return data to the client .
among : The server level character set may be different from the current database character set .
3.3. View the character set of the database
show create database [ Database name ];

3.3.1. Modify the character set of the database
alter database [ database ] character set [ Character set name ];
3.4. View the character set of the data table
# Modify the datasheet character set
alter table [ Data sheet ] character set [ Character set name ];
# View datasheet character set
show create database [ Database name ];

4. Character sets at all levels
MySQL Yes 4 A level of character set and more regular , Namely :
- Server level : from character_set_server Appoint
- Database level : The database level character set follows the server level character set by default , You can also specify different character sets
- Table level : The table level character set follows the database level character set by default , You can also specify different character sets
- Column level : The column level character set defaults to the table level character set , You can also specify different character sets
4.1. Server level
character_set_server: Server level character set
We can use the startup option when starting the server program or when the server program is running set Statement to modify the values of these two variables , For example, we can use the configuration file /etc/my.cnf writes :
character-set-server=utf8
collation-server=utf8_unicode_ci
4.2. Database level
character_set_database : The character set of the current database
We can specify character sets and comparison rules when creating and modifying databases , The specific syntax is as follows :
CREATE DATABASE Database name
[[DEFAULT] CHARACTER SET Character set name ]
[[DEFAULT] COLLATE Comparison rule name ]
ALTER DATABASE Database name
[[DEFAULT] CHARACTER SET Character set name ]
[[DEFAULT] COLLATE Comparison rule name ]
Among them DEFAULT It can be omitted , It does not affect the semantics of the statement , such as :
mysql> create database demodb1
-> character set utf8
-> collate utf8_unicode_ci;
Query OK, 1 row affected (0.01 sec)
4.3. Table level
We can also specify the character set and comparison rules of the table when creating and modifying the table , The grammar is as follows :
CREATE TABLE Table name ( Column information )
[[DEFAULT] CHARACTER SET Character set name ]
[[DEFAULT] COLLATE Comparison rule name ]
ALTER TABLE Table name ( Column information )
[[DEFAULT] CHARACTER SET Character set name ]
[[DEFAULT] COLLATE Comparison rule name ]
Creating a table is similar to creating a database , among DEFAULT You can omit it . such as ;
mysql> create table t1(id int,name varchar(50))
-> character set utf8mb4
-> collate utf8mb4_unicode_ci;
Query OK, 0 rows affected (0.01 sec)
4.4. Column level
For columns that store strings , Different columns in the same table can also have different character sets and comparison rules , When we create and modify a column definition, we can specify the character set and comparison rules of the column , The grammar is as follows :
CREATE TABLE Table name (
Name String type [CHARACTER SET Character set name ][COLLATE Comparison rule name ],
Other columns ....
)
ALTER TABLE Table name MODIFY COLUMN Name String type [CHARACTER SET Character set name ][COLLATE Comparison rule name ];
For example, let's modify the table t1 Middle column id The character set comparison rule of can be written as follows :
ALTER TABLE t1 MODIFY COLUMN `id` int(11) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
The best practice is : Set the server level encoding to utf8 that will do , Database and data table are consistent .
5. Character sets and comparison rules
5.1. utf8 And utf8mb4
utf8 The character set indicates that a character needs to use 1~4 Bytes , But some of the characters we often use 1~3 It's just a byte , The character set represents the maximum byte length of a character , In some aspects, it will affect the storage and performance of the system , So it's about MySQL Our designers secretly defined two concepts :
utf8mb3: Castrated utf8 Character set , Use only1~3Bytes represent charactersutf8mb4: authentic utf8 Character set , Use1~4Bytes represent characters .
stay MySQL in utf8 yes utf8mb3 Another name for , So after that MySQL I mentioned utf8 It means using1~3A byte represents a character . If you use 4 The case of byte encoding a character , For example, store someemoji expression, Then please useutf8mb4.
You can view the following commands MySQL Supported character sets :
SHOW CHARSET;

5.2. Compare the rules
The above table ,MySQL The version supports 41 A character set , Among them Default collation Columns represent a default comparison rule in this character set , It contains the language in which the comparison rule mainly works , such as :utf8_polish_ci It means to compare the rules in Polish ,utf8_general_ci It's a general rule of comparison .
The suffix indicates whether the comparison rule distinguishes accents in the language , Case write , As follows :
| suffix | English interpretation | describe |
|---|---|---|
| _ai | accent insensitive | Stress insensitive |
| _as | accent sensitive | Distinguish between stress |
| _ci | case insensitve | Case insensitive |
| _cs | case sensitive | Case sensitive |
| _bin | binary | Compare in binary terms |
The last column Maxlen, It represents this kind of character set, indicating that a character needs up to a few bytes .
| Character set name | Maxlen |
|---|---|
| ascill | 1 |
| latin1 | 1 |
| gb2312 | 2 |
| gbk | 2 |
| utf8 | 3 |
| utf8mb4 | 4 |
Common operations :
# see gbk Comparison rules for character sets
SHOW COLLATION LIKE 'gbk%'
# see utf8 Comparison rules for character sets
SHOW COLLATION LIKE 'utf8%'
summary
This article is also a basic article , In detail MySQL Character set and comparison rules in .
边栏推荐
- 【团队管理】测试团队绩效管理的25点小建议
- Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
- Qt 中发送自定义事件
- 解决:模型训练时loss出现nan
- There was an error checking the latest version of pip
- Telnet port login method with user name for liunx server
- 基于QingCloud的地理信息企业研发云解决方案
- Mysql数据(Liunx环境)定时备份
- The pie chart with dimension lines can set various parameter options
- Numpy 中的方法汇总
猜你喜欢

一文详解|增长那些事儿

It is enough to read this article about ETL. Three minutes will let you understand what ETL is

为什么ping不通,而traceroute却可以通

MySQL | 存储《康师傅MySQL从入门到高级》笔记

Liunx Mysql安装

every()、map()、forEarch()方法。数组里面有对象的情况

Become an IEEE student member

Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)

K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream

Prompt code when MySQL inserts Chinese data due to character set problems: 1366
随机推荐
Qt 中发送自定义事件
【NOI模拟赛】寄(树形DP)
Rsync for file backup
PHP code encryption + extended decryption practice
什么是图神经网络?图神经网络有什么用?
Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
110. 平衡二叉树-递归法
xtrabackup做数据备份
数据中台:数据中台全栈技术架构解析,附带行业解决方案
MySQL | 存储《康师傅MySQL从入门到高级》笔记
關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL
数据中台:中台实践与总结
数据中台:数据治理概述
一文详解|增长那些事儿
The form image uploaded in chorme cannot view the binary image information of the request body
ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
China chip Unicorn Corporation
小程序云数据,数据请求一个集合数据的方法
every()、map()、forEarch()方法。数组里面有对象的情况