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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM