1 概述
1. 字符集(character set)
(1) '字母' 和 '数字' 的集合
(2) 支持不同语言(如:通用 utf-8, 中文 gbk)
(3) 存储字节长度不同(select length('a'), length('我');)
2. 校验(show collation) -- 又称 '排序规则'
(1) 每种字符集都可能有多种校验规则,并且有一个默认的
(2) 可以给 '表' or '列' 加校验
(3) 常用于 order by、group by、having 等
2 管理
2.1 创建表
use test;
drop table if exists student_info;
create table student_info (
sno int unsigned not null auto_increment comment '学号',
name varchar(50) not null comment '姓名',
create_date date not null comment '创建日期',
create_user varchar(50) not null comment '创建人',
update_date date not null comment '更新日期',
update_user varchar(50) not null comment '更新人',
primary key (sno)
) engine = innodb
character set = utf8 collate = utf8_general_ci comment ='学生信息表';
-- 看 👆 👆 --
查看表的 ddl 语句:
show create table test.student_info;
2.1 字符集 character set
查询方式:
> show character set;
> select * from information_schema.character_sets;
>
> show variables like 'character_set%';
明细解释:
1. show character set; -- 所支持的字符集
Charset Description Default collation Maxlen
utf-8 UTF-8 Unicode utf8_general_ci 3
gbk gbk_chinese_ci 2
...
2. show variables like 'character_set%'; -- 字符集支持的层级
(1) character_set_client 告诉服务器发送的数据是什么类型的
(2) character_set_connection 告诉转换器,转换什么编码
(3) character_set_results 查询结果用什么编码
-- 若以上三者均为字符集 N,可简写为 set names N;
2.2 校对 show collation
查询方式:
> show collation;
> select * from information_schema.collations;
明细解释:
1. show collation;
Collation Charset
gbk_chinese_ci gbk
utf8_bin utf8
latin2_czech_cs latin2
'校对规则'(或称 '排序规则')
_ci : 不区分大小写
_cs : 区分大小写
_bin: 二进制
2. show variables like 'collation%'; -- 校验规则的层级
3 扩展
3.1 查询表的校验规则
select t.table_name,
t.table_comment,
t.table_collation
from information_schema.tables t
where t.table_schema = database()
order by 1, 3;
3.2 查询列的校验规则
select c.table_name,
c.column_name,
c.column_comment,
c.collation_name
from information_schema.`columns` c
where c.table_schema = database()
order by 1, 2, 4;