本篇內(nèi)容介紹了“MySQL字符集和校對(duì)規(guī)則是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
安溪網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,安溪網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為安溪數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的安溪做網(wǎng)站的公司定做!
查看所有可用字符集:
mysql> show character set;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
查看字符集(character):存儲(chǔ)方式 和 校對(duì)規(guī)則(collation):比較字符串的方式
select * from information_schema.character_sets
校對(duì)規(guī)則
mysql> show collation like '%gbk%';
+----------------+---------+----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+----------------+---------+----+---------+----------+---------+
| gbk_chinese_ci | gbk | 28 | Yes | Yes | 1 |
| gbk_bin | gbk | 87 | | Yes | 1 |
+----------------+---------+----+---------+----------+---------+
2 rows in set (0.00 sec)
命名約定:
字符集名開(kāi)始+:
_ci (大小寫(xiě)不敏感)
_cs(大小寫(xiě)敏感)
_bin(二元,比較字符編碼值,對(duì)大小寫(xiě)不敏感)
例:
select case when 'A' COLLATE utf8_general_ci='a' collate utf8_general_ci then 1 else 0 end;
1
--校對(duì)規(guī)則是ci時(shí)認(rèn)為A和a相同
select case when 'A' COLLATE utf8_bin='a' collate utf8_bin then 1 else 0 end;
0
Mysql字符集的設(shè)置
SERVER服務(wù)器級(jí)別、DB數(shù)據(jù)庫(kù)級(jí)、TABLE表級(jí)、字段級(jí)
SERVER服務(wù)器字符集和校對(duì)規(guī)則
1. my.cnf (ini) 設(shè)置
[mysql]
character-set-server=utf8
2. 啟動(dòng)選項(xiàng)
mysqld --character-set-server=utf8
3. 編譯時(shí)指定
cmake . –DDEFAULT_CHARSET=utf8
如果以上都沒(méi)有指定則使用:latin1
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)
DB數(shù)據(jù)庫(kù)字符集合校對(duì)規(guī)則
在數(shù)據(jù)庫(kù)創(chuàng)建時(shí)指定,可以alter database修改,但是已有的數(shù)據(jù)不會(huì)修改
1>如果指定了DB字符集和校對(duì)則使用
2>如果指定了DB字符集沒(méi)指定校對(duì),使用字符集默認(rèn)校對(duì)
3>如果指定了DB校對(duì)沒(méi)指定字符集,使用校對(duì)相關(guān)字符集
4>如果沒(méi)有設(shè)定DB字符集和校對(duì),使用server字符集和校對(duì)
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)
Table表字符集和校對(duì)
CREATE TABLE `t` (
`id` varchar(100) DEFAULT NULL,
`context` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8
創(chuàng)建table時(shí)指定,如果未設(shè)置根據(jù)db字符集
連接字符集
character_set_client 客戶(hù)端字符集
character_set_connection 連接字符集
character_ser_results 返回字符集
這3個(gè)參數(shù)確保相同,保證寫(xiě)入讀出都正常
設(shè)置:
[mysql]
default-character-set=utf8
可統(tǒng)一設(shè)置:
Set names utf8
強(qiáng)制設(shè)置字符集
select _utf8 '字符集',_big5 '字符集'
“MySQL字符集和校對(duì)規(guī)則是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!