• mysql 中 character set 与 collation 的理解


    character set 和 collation 的是什么?

    character set, 即字符集。

    我们常看到的 utf-8, GB2312, GB18030 都是相互独立的 character set. 即对 Unicode 的一套编码。

    如何理解 unicode 与 utf-8, GB2312 的区别呢?
    打个比方,你眼前有一个苹果,在英文里称之为 apple, 而在中文里称之为苹果。
    苹果这个实体的概念就是 unicode , 而 utf-8, GB2312 可以认为就是不同语言对苹果的不同称谓,本质上都是在描述苹果这个物。

    collation,英译[核对 ]  即比对方法,用于指定数据集如何排序,以及字符串的比对规则。

    character set 与 collation 的关系

    软件国际化是大势所趋, 所以 unicode 是国际化最佳的选择。

    为了提高性能,有些情况下还是使用 latin1 比较好。

    mysql 有两个支持 unicode 的 character set:

    1. ucs2: 使用 16 bits 来表示一个 unicode 字符。

    2. utf8: 使用 1~3 bytes 来表示一个 unicode 字符。

    选择哪个 character set 视情况而定,例如 utf8 表示 latin 字符只需要一个字节,所以当用户数据大部分为英文等拉丁字符时,使用 utf8 比较节省数据库的存储空间。据说 SQL Server 采用的是 ucs2, 我表示怀疑。

    每个 character set 会对应一定数量的 collation. 查看方法是在 mysql 的 console 下输入:

    查询代码 mysql> show collation;  

    我们会看到这样的结果:

    collation 名字的规则可以归纳为这两类:

    1. <character set>_<language/other>_<ci/cs>

    2. <character set>_bin

    例如:utf8_danish_ci

    ci 是 case insensitive 的缩写, cs 是 case sensitive 的缩写。即,指定大小写是否敏感。ci即对大小写不敏感

    奇怪的是 utf8 字符集对应的 collation 居然没有一个是 cs 的。

    那么 utf8_general_ci, utf8_unicode_ci, utf8_danish_ci 有什么区别? 他们各自存在的意义又是什么?

    同一个 character set 的不同 collation 的区别在于排序、字符春对比的准确度(相同两个字符在不同国家的语言中的排序规则可能是不同的)以及性能。

    例如:

    utf8_general_ci 在排序的准确度上要逊于 utf8_unicode_ci, 当然,对于英语用户应该没有什么区别。但性能上(排序以及比对速度)要略优于 utf8_unicode_ci. 例如前者没有对德语中ß = ss的支持。

    而 utf8_danish_ci 相比 utf8_unicode_ci 增加了对丹麦语的特殊排序支持。

    补充:

    1. 当表的 character set 是 latin1 时,若字段类型为 nvarchar, 则字段的字符集自动变为 utf8.

    可见 database character set, table character set, field character set 可逐级覆盖,

    类似于面向对象中子类继承父类,重写父类的方法。

    2. 在 ci 的 collation 下,如何在比对时区分大小写

    mysql> select * from pet;
    
    | name | owner | species | sex | birth | death |
    +----------+-------+---------+------+------------+-------+
    | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
    | whistler | Gwen | bird | NULL | 1988-09-25 | NULL |
    
    
    mysql> select * from pet where name = 'whistler';
    
    | name | owner | species | sex | birth | death |
    +----------+-------+---------+------+------------+-------+
    | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
    | whistler | Gwen | bird | NULL | 1988-09-25 | NULL |
    
    
    mysql> select * from pet where binary name = 'whistler';
    +----------+-------+---------+------+------------+-------+
    | name | owner | species | sex | birth | death |
    +----------+-------+---------+------+------------+-------+
    | whistler | Gwen | bird | NULL | 1988-09-25 | NULL |
    
    
    mysql> select * from pet where name = binary 'whistler';
    +----------+-------+---------+------+------------+-------+
    | name | owner | species | sex | birth | death |
    +----------+-------+---------+------+------------+-------+
    | whistler | Gwen | bird | NULL | 1988-09-25 | NULL |
    

    推荐使用

    mysql> select * from pet where name = binary 'whistler';

    这样可以保证当前字段的索引依然有效, 而下面这种会使索引失效

    mysql> select * from pet where binary name = 'whistler';   

    原文链接:http://www.360doc.com/content/11/0303/01/2588264_97631236.shtml

  • 相关阅读:
    bzoj 1179[Apio2009]Atm (tarjan+spfa)
    tarjan讲解(用codevs1332(tarjan的裸题)讲解)
    spfa
    codevs 1021 玛丽卡(spfa)
    Redis-mac安装与使用
    心血来潮整理一下STL——string
    基础算法整理(1)——递归与递推
    一道水的不能再水的中国盒子问题——小试牛刀(3)
    论久违的八数码问题——小试牛刀(2)
    8皇后及其普及(N)——小试牛刀(1)
  • 原文地址:https://www.cnblogs.com/chenjfblog/p/7686018.html
Copyright © 2020-2023  润新知