当前位置:网站首页>[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

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 Hat4.8.5-39
MySQL5.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

 Insert picture description here
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%';

 Insert picture description here
among :

  1. character_set_client: The character set used by the server to decode the request
  2. character_set_connection: When the server processes the request, it will change the request string from character_set_client turn character_set_connection
  3. character_set_database: The character set of the current database
  4. character_set_server: Server level character set
  5. 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 ];

 Insert picture description here

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 ];

 Insert picture description here

4. Character sets at all levels

MySQL Yes 4 A level of character set and more regular , Namely :

  1. Server level : from character_set_server Appoint
  2. Database level : The database level character set follows the server level character set by default , You can also specify different character sets
  3. Table level : The table level character set follows the database level character set by default , You can also specify different character sets
  4. 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 :

  1. utf8mb3 : Castrated utf8 Character set , Use only 1~3 Bytes represent characters
  2. utf8mb4: authentic utf8 Character set , Use 1~4 Bytes represent characters .
    stay MySQL in utf8 yes utf8mb3 Another name for , So after that MySQL I mentioned utf8 It means using 1~3 A byte represents a character . If you use 4 The case of byte encoding a character , For example, store some emoji expression , Then please use utf8mb4.
    You can view the following commands MySQL Supported character sets :
SHOW CHARSET;

 Insert picture description here

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
_aiaccent insensitive Stress insensitive
_asaccent sensitive Distinguish between stress
_cicase insensitve Case insensitive
_cscase sensitive Case sensitive
_binbinary 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
ascill1
latin11
gb23122
gbk2
utf83
utf8mb44

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 .

原网站

版权声明
本文为[Man Nong Feige]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240635532924.html