数据库
首页 > 数据库> > MySQL基础知识:Character Set和Collation

MySQL基础知识:Character Set和Collation

作者:互联网

A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set.

操作系统环境为MacOS Catalina, MySQL版本为: 8.0.13 MySQL Community Server - GPL

MySQL Charset和Collation简述

Collation后缀

Suffix Meaning
_ai Accent-insensitive
_as Accent-sensitive
_ci Case-insensitive
_cs Case-sensitive
_ks Kana-sensitive
_bin Binary

MySQL Server的Charset和Collation

查看MySQL Server支持的Charset

有很多种方法可以查看当前MySQL Server支持的Character Set:

show character set; -- 方法1

show charset; -- 方法2

show char set; -- 方法3

select * from information_schema.character_sets; -- 方法 4

查看特定字符集信息(主要包含默认的Collation和MAXLEN):

show character set like 'utf%';; -- 方法1

show charset like 'utf%';; -- 方法2

show char set like 'utf%'; -- 方法3

select * from information_schema.character_sets 
where CHARACTER_SET_NAME like 'utf%'; -- 方法 4

查看MySQL Server支持的Collation

SHOW COLLATION WHERE Charset = 'utf8mb4';

或者

select * from INFORMATION_SCHEMA.COLLATIONS where CHARACTER_SET_NAME='utf8mb4';

查看MySQL Server当前的Charset和Collation

show variables like 'character_set_server';
show variables like 'collation_server';

或者使用语句:

select @@character_set_server, @@collation_server;

MySQL Server默认的Charset和Collation

MySQL官方文档可查看默认的Charset和Collation:

修改MySQL Server默认的Charset和Collation

修改默认值,需要重新编译源码。

cmake . -DDEFAULT_CHARSET=latin1

或者

cmake . -DDEFAULT_CHARSET=latin1 \
  -DDEFAULT_COLLATION=latin1_german1_ci

指定MySQL Server的Charset和Collation

可以在启动MySQL Server的时候指定Server的Charset和Collation,下面三种方法是等效的:

mysqld # 默认的charset是utf8mb4, 而且 utf8mb4 默认的collation是 utf8mb4_0900_ai_ci

mysqld --character-set-server=utf8mb4

mysqld --character-set-server=utf8mb4 \
  --collation-server=utf8mb4_0900_ai_ci

数据库(Database)的Charset和Collation

创建数据库的时候,如果没有指定character setcollation,会自动用MySQL Server的 character setcollation

查看数据的Character Set和Collation

USE db_name;
SELECT @@character_set_database, @@collation_database;

不想改变当前数据库的话,可以使用语句:

SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME
FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'db_name';

指定或修改数据库(Database)的Character Set和Collation

创建db时指定:

CREATE DATABASE db_name CHARACTER SET latin1 COLLATE latin1_swedish_ci;

修改:

ALTER DATABASE db_name CHARACTER SET latin1 COLLATE latin1_swedish_ci;

表(Table)的Charset和Collation

如果创建表的时候没有指定表级别Charset和Collation,会默认使用数据库(Database)的Charset和Collation。

查看表(Table)的Charset和Collation

SELECT 
t.TABLE_SCHEMA,
t.table_name,
ccsa.* 
FROM 
information_schema.`TABLES` t,
information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` ccsa
WHERE ccsa.collation_name = t.table_collation
AND t.table_schema = "db_name"
AND t.table_name = "table_name";

指定或修改表(Table)的Character Set和Collation

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]

列(Column)的Charset和Collation

如果创建表的时候没有指定列的Charset和Collation,会默认使用表(Table)的Charset和Collation。

查看列(Column)的Charset和Collation

SELECT 
*
FROM information_schema.`COLUMNS`
WHERE table_schema = "db_name"
AND table_name = "table_name";

指定或修改列(Column)的Character Set和Collation

CREATE TABLE t1
(
    col1 CHAR(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci
) CHARACTER SET latin1 COLLATE latin1_bin;

ALTER TABLE t1 MODIFY
    col1 VARCHAR(5)
      CHARACTER SET latin1
      COLLATE latin1_swedish_ci;

Connection Character Sets and Collations

Client和MySQL Server交互前,先建立连接(Connection)。

Client通过建立的connection发送SQL Statements(查询、插入等操作)到MySQL Server;MySQL Server则通过建立的connection返回给Client相应的结果(SQL执行结果,或者错误信息)。

Client和Server建立连接并设置character set

  1. Client在建立连接的时候会指定collation (charset的默认collation);
  2. MySQL Server根据collation找到对应的charset;
  3. MySQL Server然后charset设置session变量: character_set_client, character_set_results, character_set_connectioncollation_connection被设置为指定charset的默认collation。

相关session变量

查看connection相关的session变量:

SHOW SESSION VARIABLES LIKE 'character\_set\_%';
SHOW SESSION VARIABLES LIKE 'collation\_%';

设置character set和collation

SET NAMES {'charset_name'
    [COLLATE 'collation_name'] | DEFAULT}

SET NAME 会设置三个session变量(session system variables)为指定的charset和collation:

SET {CHARACTER SET | CHARSET}
    {'charset_name' | DEFAULT}

SET CHARACTER SET会设置 character_set_clientcharacter_set_results为指定的charset;

并把 character_set_connection 设置为 character_set_database 的charset。

参考文档

  1. What is Collation and Character Set in MySQL?
  2. Character Sets, Collations, Unicode
  3. What does character set and collation mean exactly?
  4. MySQL Character Set
  5. MySQL Collation
  6. Connection Character Sets and Collations

原文:MySQL基础知识:Character Set和Collation

标签:set,Character,character,Server,collation,Set,MySQL,Collation
来源: https://www.cnblogs.com/codesee/p/14519975.html