mysql 中 character set 與 collation 的理解
目錄
- character set 和 collation 的是什麼?
- 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 下輸入:
我們會看到這樣的結果:
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 * 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 |
|
推薦使用
1 |
mysql> select * from pet where name = binary 'whistler' ;
|
這樣可以保證當前欄位的索引依然有效, 而下面這種會使索引失效。
1 |
mysql> select * from pet where binary name = 'whistler' ;
|
原文連結:http://www.360doc.com/content/11/0303/01/2588264_97631236.shtml
**************************************************************************************當你的才華還撐不起你的野心的時候,你就應該靜下心來學習;當你的能力還駕馭不了你的目標時,就應該沉下心來,歷練;夢想,不是浮躁,而是沉澱和積累,只有拼出來的美麗,沒有等出來的輝煌,機會永遠是留給最渴望的那個人,學會與內心深處的你對話,問問自己,想 要怎樣的人生,靜心學習,耐心沉澱,送給自己,共勉。
**************************************************************************************