Mysql 字符集(character set) 和 校对(collate)详解

1 概述

1. 字符集(character set(1) '字母''数字' 的集合
   (2) 支持不同语言(如:通用 utf-8, 中文 gbk)
   (3) 存储字节长度不同(select length('a'), length('我');2. 校验(show collation) -- 又称 '排序规则'
   (1) 每种字符集都可能有多种校验规则,并且有一个默认的
   (2) 可以给 '表' or '列' 加校验
   (3) 常用于 order bygroup byhaving

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;
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼丸丶粗面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值