mysql字符集相關(guān)總結(jié)
這邊筆記主要記錄一下對mysql字符集相關(guān)知識的學(xué)習(xí)
字符集
一般我們使用如下語句創(chuàng)建一張表,創(chuàng)建表的時候指定了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 查看當(dāng)前mysql支持的所有字符集。
例如下圖就是我的mysql支持的所有字符集。

可以從上圖中看到,第四列表示該字符集最大長度。我們建表時常用的 utf8 在mysql里面實際最大長度是 3 個字符。一般認(rèn)知中utf8是用1~4個字符來表示具體的內(nèi)容的,但mysql里面的 utf8 實際是 utf8mb3 的別名,正宗的的最大用4個字符表示的在mysql中是 utf8mb4 。
注意:
utf8mb4 utf8mb4
比較規(guī)則
在上面那張圖中,第三列 Default collation 就是這些字符集默認(rèn)的比較規(guī)則。例如acsii字符集的默認(rèn)比較規(guī)則是 ascii_general_ci ,utf8字符集的默認(rèn)比較規(guī)則是 utf8_general_ci 。
其實每個字符集有很多種比較規(guī)則,例如我們看一下acsii字符集有哪些比較規(guī)則,如下圖所示
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 表示是不區(qū)分大小寫(case insensitive)。(反之 _cs 就表示大小寫敏感)
ascii_bin 表示是二進(jìn)制比較
再看一下utf8、utf8mb4字符集有哪些比較規(guī)則,如下圖所示
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 | +--------------------------+---------+-----+---------+----------+---------+
使用不同的比較規(guī)則對數(shù)據(jù)進(jìn)行比較,結(jié)果可能是不一樣的。
字符集和比較規(guī)則是對應(yīng)的,不管是只修改字符集還是只修改比較規(guī)則,另一方都會相應(yīng)地自動變化。
四個級別的字符集和比較規(guī)則
上文提到了字符集和比較規(guī)則,實際上在mysql里面有不同級別的字符集和比較規(guī)則:
- 服務(wù)器級別
- 數(shù)據(jù)庫級別
- 表級別
- 列級別
服務(wù)器級別的字符集可以看參數(shù) character_set_server ,服務(wù)器級別的比較規(guī)則可以看參數(shù) 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)
如果要查看數(shù)據(jù)庫級別的字符集和比較規(guī)則,那么可以先 use xxxdb 切換到具體的數(shù)據(jù)庫,然后執(zhí)行 show variables like 'character_set_database' 和 show variables like 'collation_database' 來查看該數(shù)據(jù)庫的字符集和比較規(guī)則。
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)
如果要查看某個表的字符集和比較規(guī)則,或者表中的某個列的字符集和比較規(guī)則,那么可以使用 show create table xxxtb 來查看具體的建表語句,里面就有使用的字符集和比較規(guī)則。
如果創(chuàng)建數(shù)據(jù)庫的時候沒有指定字符集和比較規(guī)則,則會使用服務(wù)器級別的字符集和比較規(guī)則作為數(shù)據(jù)庫的字符集和比較規(guī)則。如果創(chuàng)建和修改表的語句中沒有指明字符集和比較規(guī)則,將使用該表所在數(shù)據(jù)庫的字符集和比較規(guī)則作為該表的字符集和比較規(guī)則
三個系統(tǒng)變量
mysql服務(wù)器可以對應(yīng)多個客戶端,而這些客戶端的編碼方式可能不同,但存在數(shù)據(jù)庫里的數(shù)據(jù)的字符集是一定的,mysql就要針對不同的客戶端進(jìn)行編碼轉(zhuǎn)換處理,然后返回結(jié)果給客戶端。
在mysql里面,有三個 SESSION級別 的系統(tǒng)變量,可以進(jìn)行上述操作:
- character_set_client : 服務(wù)器解碼客戶端請求時使用的字符集
- character_set_connection : 服務(wù)器處理請求時會把請求字符串從character_set_client轉(zhuǎn)為character_set_connection
- character_set_results : 服務(wù)器向客戶端返回數(shù)據(jù)時使用的字符集
mysql服務(wù)器會為每個客戶端都維護(hù)一份session級別的這三個變量。
不過在實際生產(chǎn)環(huán)境中,為了避免麻煩,公司一般都會有規(guī)范規(guī)定客戶端默認(rèn)都使用utf8字符集。
以上就是mysql字符集相關(guān)總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于mysql字符集的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
計算機(jī)二級考試MySQL??键c(diǎn) 8種MySQL數(shù)據(jù)庫設(shè)計優(yōu)化方法
這篇文章主要為大家詳細(xì)介紹了計算機(jī)二級考試MySQL??键c(diǎn),詳細(xì)介紹8種MySQL數(shù)據(jù)庫設(shè)計優(yōu)化方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
mysql 批處理文件出錯后繼續(xù)執(zhí)行的實現(xiàn)方法
下面小編就為大家?guī)硪黄猰ysql 批處理文件出錯后繼續(xù)執(zhí)行的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
2022最新版MySQL 8.0.30 安裝及配置教程(小白入門)
這篇文章主要介紹了2022最新版MySQL 8.0.30 安裝及配置教程,安裝過程算是比較簡單的,今天給大家分享的此文比較適合mysql數(shù)據(jù)庫的小白,需要的朋友可以參考下2022-09-09
淺談mysql數(shù)據(jù)庫中的換行符與textarea中的換行符
下面小編就為大家?guī)硪黄獪\談mysql數(shù)據(jù)庫中的換行符與textarea中的換行符。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
MySQL decimal unsigned更新負(fù)數(shù)轉(zhuǎn)化為0
這篇文章主要介紹了MySQL decimal unsigned更新負(fù)數(shù)轉(zhuǎn)化為0,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12

