佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > mysql字符集相關(guān)總結(jié)

mysql字符集相關(guān)總結(jié)

熱門標(biāo)簽:Linux服務(wù)器 AI電銷 鐵路電話系統(tǒng) 百度競價排名 地方門戶網(wǎng)站 服務(wù)外包 呼叫中心市場需求 網(wǎng)站排名優(yōu)化

這邊筆記主要記錄一下對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)文章!

您可能感興趣的文章:
  • MySQL修改字符集的實戰(zhàn)教程
  • MYSQL字符集設(shè)置的方法詳解(終端的字符集)
  • 詳解mysql中的字符集和校驗規(guī)則
  • MySQL查看與修改字符集的方法實例教程
  • 如何修改MySQL字符集
  • mysql數(shù)據(jù)庫中字符集亂碼問題原因及解決
  • MySQL字符集utf8修改為utf8mb4的方法步驟
  • mysql如何在已有數(shù)據(jù)庫上統(tǒng)一字符集
  • MySQL字符集亂碼及解決方案分享
  • 詳解JDBC對Mysql utf8mb4字符集的處理
  • MAC下修改mysql默認(rèn)字符集為utf8的方法

標(biāo)簽:仙桃 黃山 崇左 衡水 湘潭 蘭州 湖南 銅川

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mysql字符集相關(guān)總結(jié)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    清河县| 九龙城区| 故城县| 武平县| 瑞金市| 伊宁县| 隆昌县| 格尔木市| 新泰市| 铜山县| 玉环县| 堆龙德庆县| 平阴县| 衡山县| 项城市| 曲阜市| 晋中市| 天祝| 罗田县| 金沙县| 奉贤区| 牙克石市| 维西| 固安县| 怀宁县| 揭阳市| 赣州市| 吕梁市| 阳信县| 武义县| 平陆县| 四子王旗| 玉屏| 连山| 秭归县| 崇信县| 洱源县| 霍林郭勒市| 丹棱县| 桐乡市| 申扎县|