首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

mysql 中 character set 与 collation 的点滴了解

2012-08-14 
mysql 中 character set 与 collation 的点滴理解使用 mysql 创建数据表的时候, 总免不了要涉及到 charact

mysql 中 character set 与 collation 的点滴理解

使用 mysql 创建数据表的时候, 总免不了要涉及到 character set 和 collation 的概念, 之前不是很了解。

?

这两天不是很忙, 就自己整理了一下。

?

?

先来看看 character set 和 collation 的是什么?

&. character set, 即字符集。

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

?

?

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

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

2. <character set>_bin

?

例如:

utf8_danish_ci

?

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

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

?

mysql 中 character set 与 collation 的点滴了解

?

?

那么 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';

?

会使索引失效。

?

?

参考列表:

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

热点排行