1. 什么是字符集和校驗(yàn)規(guī)則?
字符集是一套符號(hào)和編碼。校對(duì)規(guī)則是在字符集內(nèi)用于比較字符的一套規(guī)則。任何一個(gè)給定的字符集至少有一個(gè)校對(duì)規(guī)則,它可能有幾個(gè)校對(duì)規(guī)則。要想列出一個(gè)字符集的校對(duì)規(guī)則,使用SHOW COLLATION語(yǔ)句。
校對(duì)規(guī)則一般有這些特征:
2. 不同級(jí)別的字符集和校驗(yàn)規(guī)則可控制大小寫(xiě)敏感
MySQL5.1在同一臺(tái)服務(wù)器、同一個(gè)數(shù)據(jù)庫(kù)或甚至在同一個(gè)表中使用不同字符集或校對(duì)規(guī)則來(lái)混合定義字符串。字符集和校對(duì)規(guī)則有4個(gè)級(jí)別的默認(rèn)設(shè)置:服務(wù)器級(jí)、數(shù)據(jù)庫(kù)級(jí)、表級(jí)和連接級(jí)。
2.1服務(wù)器級(jí)
MySQL按照如下方法確定服務(wù)器字符集和服務(wù)器校對(duì)規(guī)則:
當(dāng)啟動(dòng)mysqld時(shí),根據(jù)使用的初始選項(xiàng)設(shè)置來(lái)確定服務(wù)器字符集和校對(duì)規(guī)則。
shell> mysqld --default-character-set=latin1 --default-collation=latin1_swedish_ci
更改設(shè)定值的一個(gè)方法是通過(guò)重新編譯。如果希望在從源程序構(gòu)建時(shí)更改默認(rèn)服務(wù)器字符集和校對(duì)規(guī)則,使用:--with-charset和--with-collation作為configure的參量。例如:
shell> ./configure --with-charset=latin1 --with-collation=latin1_german1_ci
mysqld和configure都驗(yàn)證字符集/校對(duì)規(guī)則組合是否有效。如果無(wú)效,每個(gè)程序都顯示一個(gè)錯(cuò)誤信息,然后終止。
2.2數(shù)據(jù)庫(kù)級(jí)
MySQL這樣選擇數(shù)據(jù)庫(kù)字符集和數(shù)據(jù)庫(kù)校對(duì)規(guī)則:
mysql> create database yutest default character set utf8 collate utf8_bin;Query OK, 1 row affected (0.00 sec)mysql> show variables like 'collation_%';+----------------------+-----------------+| Variable_name| Value |+----------------------+-----------------+| collation_connection | utf8_general_ci || collation_database | utf8_bin|| collation_server | utf8_general_ci |+----------------------+-----------------+3 rows in set (0.00 sec)mysql> select * from t1;+------+| name |+------+| ABC|| abc|+------+2 rows in set (0.00 sec)mysql> select * from t1 where name='abc';+------+| name |+------+| abc|+------+1 row in set (0.01 sec)
可以看出,在數(shù)據(jù)庫(kù)級(jí)進(jìn)行相應(yīng)的校對(duì)規(guī)則設(shè)置,查詢(xún)大小寫(xiě)敏感。
2.3表級(jí)
MySQL按照下面的方式選擇表字符集和 校對(duì)規(guī)則:
mysql> create database yutest2;Query OK, 1 row affected (0.01 sec)mysql> use yutest2;Database changedmysql> create table t1(name varchar(10)) -> default character set utf8 collate utf8_bin;Query OK, 0 rows affected (0.01 sec)mysql> insert into t1 values('ABC');Query OK, 1 row affected (0.00 sec)mysql> insert into t1 values('abc');Query OK, 1 row affected (0.00 sec)mysql> show variables like 'collation_%';+----------------------+-----------------+| Variable_name | Value |+----------------------+-----------------+| collation_connection | utf8_general_ci || collation_database | utf8_general_ci || collation_server | utf8_general_ci |+----------------------+-----------------+3 rows in set (0.00 sec)mysql> select * from t1;+------+| name |+------+| ABC|| abc|+------+2 rows in set (0.00 sec)mysql> select * from t1 where name='abc';+------+| name |+------+| abc|+------+1 row in set (0.00 sec)
可以看出,在表級(jí)進(jìn)行相應(yīng)的校對(duì)規(guī)則設(shè)置,查詢(xún)大小寫(xiě)敏感。
2.4 連接級(jí)
考慮什么是一個(gè)“連接”:它是連接服務(wù)器時(shí)所作的事情。客戶(hù)端發(fā)送SQL語(yǔ)句,例如查詢(xún),通過(guò)連接發(fā)送到服務(wù)器。服務(wù)器通過(guò)連接發(fā)送響應(yīng)給客戶(hù)端,例如結(jié)果集。對(duì)于客戶(hù)端連接,這樣會(huì)導(dǎo)致一些關(guān)于連接的字符集和校對(duì)規(guī)則的問(wèn)題,這些問(wèn)題均能夠通過(guò)系統(tǒng)變量來(lái)解決:
mysql> show variables like 'character%';+--------------------------+----------------------------+| Variable_name| Value|+--------------------------+----------------------------+| character_set_client | utf8 || character_set_connection | utf8 || character_set_database | utf8 || character_set_filesystem | binary || character_set_results| utf8 || character_set_server | utf8 || character_set_system | utf8 || character_sets_dir | /usr/share/mysql/charsets/ |+--------------------------+----------------------------+8 rows in set (0.00 sec)
服務(wù)器使用character_set_client變量作為客戶(hù)端發(fā)送的查詢(xún)中使用的字符集。
轉(zhuǎn)換時(shí),服務(wù)器使用character_set_connection和collation_connection系統(tǒng)變量。它將客戶(hù)端發(fā)送的查詢(xún)從character_set_client系統(tǒng)變量轉(zhuǎn)換到character_set_connection。
character_set_results變量指示服務(wù)器返回查詢(xún)結(jié)果到客戶(hù)端使用的字符集。包括結(jié)果數(shù)據(jù),例如列值和結(jié)果元數(shù)據(jù)(如列名)。
3. 如果設(shè)置的是大小寫(xiě)不敏感的校驗(yàn)規(guī)則,仍然有方法區(qū)分查詢(xún)的大小寫(xiě)
3.1 在SQL語(yǔ)句中使用collate
使用collate子句,能夠?yàn)橐粋€(gè)比較覆蓋任何默認(rèn)校對(duì)規(guī)則。collate可以用于多種SQL語(yǔ)句中,比如where,having,group by,order by,as,聚合函數(shù)。
mysql> select * from t1 where name collate utf8_bin = 'ABC';+------+| name |+------+| ABC |+------+1 row in set (0.00 sec)mysql> select * from t1 where name = 'ABC';+------+| name |+------+| ABC || Abc || abc |+------+3 rows in set (0.00 sec)mysql> select * from t1;+------+| name |+------+| ABC || Abc || abc |+------+3 rows in set (0.00 sec)
3.2 binary操作符
binary操作符是collate子句的一個(gè)速記符。binary'x'等價(jià)與'x'collate y,這里y是字符集'x'二元校對(duì)規(guī)則的名字。每一個(gè)字符集有一個(gè)二元校對(duì)規(guī)則。例如,latin1字符集的二元校對(duì)規(guī)則是latin1_bin,因此,如果列a是字符集latin1,以下兩個(gè)語(yǔ)句有相同效果:
select * from t1 order by binary a;select * from t1 order by a collate latin1_bin;
mysql> select * from t1 where binary name = 'ABC';+------+| name |+------+| ABC |+------+1 row in set (0.00 sec)mysql>mysql> select * from t1 where name = 'ABC';+------+| name |+------+| ABC || Abc || abc |+------+3 rows in set (0.00 sec)
參考鏈接:
MySQL5.1參考手冊(cè) 第十章 字符集支持 https://dev.mysql.com/doc/refman/5.1/zh/charset.html#charset-collate
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com