码迷,mamicode.com
首页 > 数据库 > 详细

mysql 中 character set 与 collation 的理解

时间:2017-10-18 13:17:33      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:代码   with   编码   star   理解   too   字节   如何   database   

使用 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 下输入:

 

Java代码
  1. mysql> show collation;  

 

我们会看到这样的结果:

 

技术分享

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

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

2. <character set>_bin

 

例如:

utf8_danish_ci

 

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

奇怪的是 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 |
+----------+-------+---------+------+------------+-------+
2 rows in set (0.00 sec)

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 |
+----------+-------+---------+------+------------+-------+
2 rows in set (0.00 sec)

mysql> select * from pet where binary name = ‘whistler‘;
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| whistler | Gwen | bird | NULL | 1988-09-25 | NULL |
+----------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

mysql> select * from pet where name = binary ‘whistler‘;
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| whistler | Gwen | bird | NULL | 1988-09-25 | NULL |
+----------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

 

推荐使用

 

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

 

参考列表:

1. What is the best collation to use for mysql with php.

http://stackoverflow.com/questions/367711/what-is-the-best-collation-to-use-for-mysql-with-php

 

2. Unicode Character Sets

http://dev.mysql.com/doc/refman/5.0/en/charset-unicode-sets.html

 

3. Show Collation Syntax

http://dev.mysql.com/doc/refman/5.1/en/show-collation.html

 

4. The Binary Operator

http://dev.mysql.com/doc/refman/5.1/en/charset-binary-op.html

mysql 中 character set 与 collation 的理解

标签:代码   with   编码   star   理解   too   字节   如何   database   

原文地址:http://www.cnblogs.com/chenjfblog/p/7686018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!