mysql 中 character set 与 collation 的理解
character set, 即字符集。
我们常看到的 utf-8, GB2312, GB18030 都是相互独立的 character set. 即对 Unicode 的一套编码。
打个比方,你眼前有一个苹果,在英文里称之为 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 会对应一定数量的 collation. 查看方法是在 mysql 的 console 下输入:
我们会看到这样的结果:

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 下,如何在比对时区分大小写:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | mysql> select* frompet;| name | owner | species | sex | birth | death |+----------+-------+---------+------+------------+-------+| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL || whistler | Gwen | bird | NULL | 1988-09-25 | NULL |mysql> select* frompet wherename = '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* frompet wherebinary name = 'whistler';+----------+-------+---------+------+------------+-------+| name | owner | species | sex | birth | death |+----------+-------+---------+------+------------+-------+| whistler | Gwen | bird | NULL | 1988-09-25 | NULL |mysql> select* frompet wherename = binary 'whistler';+----------+-------+---------+------+------------+-------+| name | owner | species | sex | birth | death |+----------+-------+---------+------+------------+-------+| whistler | Gwen | bird | NULL | 1988-09-25 | NULL | | 
推荐使用
| 1 | mysql> select* frompet wherename = binary 'whistler'; | 
这样可以保证当前字段的索引依然有效, 而下面这种会使索引失效。
| 1 | mysql> select* frompet wherebinary name = 'whistler'; | 
原文链接:http://www.360doc.com/content/11/0303/01/2588264_97631236.shtml
-----------------------------------------------------------------------------------------------------------------------------------
当你的才华还撑不起你的野心的时候,你就应该静下心来学习; 当你的能力还驾驭不了你的目标时,就应该沉下心来,历练; 梦想,不是浮躁,而是沉淀和积累,只有拼出来的美丽,没有等出来的辉煌,机会永远是留给最渴望的那个人,学会与内心深处的你对话,问问自己,想 要怎样的人生,静心学习,耐心沉淀,送给自己,共勉。
-----------------------------------------------------------------------------------------------------------------------------------
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号