目錄
- 字符集
- 比較規則
- 四個級別的字符集和比較規則
- 三個系統變量
這邊筆記主要記錄一下對mysql字符集相關知識的學習
字符集
一般我們使用如下語句創建一張表,創建表的時候指定了charset為utf8編碼。
CREATE TABLE `test` (
`c1` int UNSIGNED NOT NULL AUTO_INCREMENT,
`c2` varchar(255),
`c3` char(11),
`c4` varchar(255),
PRIMARY KEY (`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
但其實mysql也支持除了utf8之外的其他字符集,可以使用命令 show charset 查看當前mysql支持的所有字符集。
例如下圖就是我的mysql支持的所有字符集。

可以從上圖中看到,第四列表示該字符集最大長度。我們建表時常用的 utf8 在mysql里面實際最大長度是 3 個字符。一般認知中utf8是用1~4個字符來表示具體的內容的,但mysql里面的 utf8 實際是 utf8mb3 的別名,正宗的的最大用4個字符表示的在mysql中是 utf8mb4 。
注意:
比較規則
在上面那張圖中,第三列 Default collation 就是這些字符集默認的比較規則。例如acsii字符集的默認比較規則是 ascii_general_ci ,utf8字符集的默認比較規則是 utf8_general_ci 。
其實每個字符集有很多種比較規則,例如我們看一下acsii字符集有哪些比較規則,如下圖所示
mysql> show collation like 'ascii%';
+------------------+---------+----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+------------------+---------+----+---------+----------+---------+
| ascii_general_ci | ascii | 11 | Yes | | 0 |
| ascii_bin | ascii | 65 | | | 0 |
+------------------+---------+----+---------+----------+---------+
ascii_general_ci 表示是一種通用的比較,其中 _ci 表示是不區分大小寫(case insensitive)。(反之 _cs 就表示大小寫敏感)
ascii_bin 表示是二進制比較
再看一下utf8、utf8mb4字符集有哪些比較規則,如下圖所示
mysql> show collation like 'utf8%';
+--------------------------+---------+-----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+--------------------------+---------+-----+---------+----------+---------+
| utf8_general_ci | utf8 | 33 | Yes | Yes | 1 |
| utf8_bin | utf8 | 83 | | Yes | 1 |
| utf8_unicode_ci | utf8 | 192 | | Yes | 8 |
| utf8_icelandic_ci | utf8 | 193 | | Yes | 8 |
| utf8_latvian_ci | utf8 | 194 | | Yes | 8 |
| utf8_romanian_ci | utf8 | 195 | | Yes | 8 |
| utf8_slovenian_ci | utf8 | 196 | | Yes | 8 |
| utf8_polish_ci | utf8 | 197 | | Yes | 8 |
| utf8_estonian_ci | utf8 | 198 | | Yes | 8 |
| utf8_spanish_ci | utf8 | 199 | | Yes | 8 |
| utf8_swedish_ci | utf8 | 200 | | Yes | 8 |
| utf8_turkish_ci | utf8 | 201 | | Yes | 8 |
| utf8_czech_ci | utf8 | 202 | | Yes | 8 |
| utf8_danish_ci | utf8 | 203 | | Yes | 8 |
| utf8_lithuanian_ci | utf8 | 204 | | Yes | 8 |
| utf8_slovak_ci | utf8 | 205 | | Yes | 8 |
| utf8_spanish2_ci | utf8 | 206 | | Yes | 8 |
| utf8_roman_ci | utf8 | 207 | | Yes | 8 |
| utf8_persian_ci | utf8 | 208 | | Yes | 8 |
| utf8_esperanto_ci | utf8 | 209 | | Yes | 8 |
| utf8_hungarian_ci | utf8 | 210 | | Yes | 8 |
| utf8_sinhala_ci | utf8 | 211 | | Yes | 8 |
| utf8_german2_ci | utf8 | 212 | | Yes | 8 |
| utf8_croatian_ci | utf8 | 213 | | Yes | 8 |
| utf8_unicode_520_ci | utf8 | 214 | | Yes | 8 |
| utf8_vietnamese_ci | utf8 | 215 | | Yes | 8 |
| utf8_general_mysql500_ci | utf8 | 223 | | Yes | 1 |
| utf8mb4_general_ci | utf8mb4 | 45 | Yes | Yes | 1 |
| utf8mb4_bin | utf8mb4 | 46 | | Yes | 1 |
| utf8mb4_unicode_ci | utf8mb4 | 224 | | Yes | 8 |
| utf8mb4_icelandic_ci | utf8mb4 | 225 | | Yes | 8 |
| utf8mb4_latvian_ci | utf8mb4 | 226 | | Yes | 8 |
| utf8mb4_romanian_ci | utf8mb4 | 227 | | Yes | 8 |
| utf8mb4_slovenian_ci | utf8mb4 | 228 | | Yes | 8 |
| utf8mb4_polish_ci | utf8mb4 | 229 | | Yes | 8 |
| utf8mb4_estonian_ci | utf8mb4 | 230 | | Yes | 8 |
| utf8mb4_spanish_ci | utf8mb4 | 231 | | Yes | 8 |
| utf8mb4_swedish_ci | utf8mb4 | 232 | | Yes | 8 |
| utf8mb4_turkish_ci | utf8mb4 | 233 | | Yes | 8 |
| utf8mb4_czech_ci | utf8mb4 | 234 | | Yes | 8 |
| utf8mb4_danish_ci | utf8mb4 | 235 | | Yes | 8 |
| utf8mb4_lithuanian_ci | utf8mb4 | 236 | | Yes | 8 |
| utf8mb4_slovak_ci | utf8mb4 | 237 | | Yes | 8 |
| utf8mb4_spanish2_ci | utf8mb4 | 238 | | Yes | 8 |
| utf8mb4_roman_ci | utf8mb4 | 239 | | Yes | 8 |
| utf8mb4_persian_ci | utf8mb4 | 240 | | Yes | 8 |
| utf8mb4_esperanto_ci | utf8mb4 | 241 | | Yes | 8 |
| utf8mb4_hungarian_ci | utf8mb4 | 242 | | Yes | 8 |
| utf8mb4_sinhala_ci | utf8mb4 | 243 | | Yes | 8 |
| utf8mb4_german2_ci | utf8mb4 | 244 | | Yes | 8 |
| utf8mb4_croatian_ci | utf8mb4 | 245 | | Yes | 8 |
| utf8mb4_unicode_520_ci | utf8mb4 | 246 | | Yes | 8 |
| utf8mb4_vietnamese_ci | utf8mb4 | 247 | | Yes | 8 |
+--------------------------+---------+-----+---------+----------+---------+
使用不同的比較規則對數據進行比較,結果可能是不一樣的。
字符集和比較規則是對應的,不管是只修改字符集還是只修改比較規則,另一方都會相應地自動變化。
四個級別的字符集和比較規則
上文提到了字符集和比較規則,實際上在mysql里面有不同級別的字符集和比較規則:
服務器級別的字符集可以看參數 character_set_server ,服務器級別的比較規則可以看參數 collation_server
mysql> show variables like 'character_set_server';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| character_set_server | utf8 |
+----------------------+-------+
1 row in set (0.00 sec)
mysql> show variables like 'collation_server';
+------------------+-----------------+
| Variable_name | Value |
+------------------+-----------------+
| collation_server | utf8_general_ci |
+------------------+-----------------+
1 row in set (0.00 sec)
如果要查看數據庫級別的字符集和比較規則,那么可以先 use xxxdb 切換到具體的數據庫,然后執行 show variables like 'character_set_database' 和 show variables like 'collation_database' 來查看該數據庫的字符集和比較規則。
Database changed
mysql> show variables like 'character_set_database';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| character_set_database | utf8 |
+------------------------+-------+
1 row in set (0.00 sec)
mysql> show variables like 'collation_database';
+--------------------+-----------------+
| Variable_name | Value |
+--------------------+-----------------+
| collation_database | utf8_general_ci |
+--------------------+-----------------+
1 row in set (0.00 sec)
如果要查看某個表的字符集和比較規則,或者表中的某個列的字符集和比較規則,那么可以使用 show create table xxxtb 來查看具體的建表語句,里面就有使用的字符集和比較規則。
如果創建數據庫的時候沒有指定字符集和比較規則,則會使用服務器級別的字符集和比較規則作為數據庫的字符集和比較規則。如果創建和修改表的語句中沒有指明字符集和比較規則,將使用該表所在數據庫的字符集和比較規則作為該表的字符集和比較規則
三個系統變量
mysql服務器可以對應多個客戶端,而這些客戶端的編碼方式可能不同,但存在數據庫里的數據的字符集是一定的,mysql就要針對不同的客戶端進行編碼轉換處理,然后返回結果給客戶端。
在mysql里面,有三個 SESSION級別 的系統變量,可以進行上述操作:
- character_set_client : 服務器解碼客戶端請求時使用的字符集
- character_set_connection : 服務器處理請求時會把請求字符串從character_set_client轉為character_set_connection
- character_set_results : 服務器向客戶端返回數據時使用的字符集
mysql服務器會為每個客戶端都維護一份session級別的這三個變量。
不過在實際生產環境中,為了避免麻煩,公司一般都會有規范規定客戶端默認都使用utf8字符集。
以上就是mysql字符集相關總結的詳細內容,更多關于mysql字符集的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:- MySQL修改字符集的實戰教程
- MYSQL字符集設置的方法詳解(終端的字符集)
- 詳解mysql中的字符集和校驗規則
- MySQL查看與修改字符集的方法實例教程
- 如何修改MySQL字符集
- mysql數據庫中字符集亂碼問題原因及解決
- MySQL字符集utf8修改為utf8mb4的方法步驟
- mysql如何在已有數據庫上統一字符集
- MySQL字符集亂碼及解決方案分享
- 詳解JDBC對Mysql utf8mb4字符集的處理
- MAC下修改mysql默認字符集為utf8的方法