查看所有可用字符集:
mysql> show character set;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
查看字符集(character):存儲方式 和 校對規則(collation):比較字符串的方式
select * from information_schema.character_sets
校對規則
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)
命名約定:
字符集名開始+:
_ci (大小寫不敏感)
_cs(大小寫敏感)
_bin(二元,比較字符編碼值,對大小寫不敏感)
例:
select case when 'A' COLLATE utf8_general_ci='a' collate utf8_general_ci then 1 else 0 end;
1
--校對規則是ci時認為A和a相同
select case when 'A' COLLATE utf8_bin='a' collate utf8_bin then 1 else 0 end;
0
Mysql字符集的設置
SERVER服務器級別、DB數據庫級、TABLE表級、字段級
SERVER服務器字符集和校對規則
1. my.cnf (ini) 設置
[mysql]
character-set-server=utf8
2. 啟動選項
mysqld --character-set-server=utf8
3. 編譯時指定
cmake . –DDEFAULT_CHARSET=utf8
如果以上都沒有指定則使用: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數據庫字符集合校對規則
在數據庫創建時指定,可以alter database修改,但是已有的數據不會修改
1>如果指定了DB字符集和校對則使用
2>如果指定了DB字符集沒指定校對,使用字符集默認校對
3>如果指定了DB校對沒指定字符集,使用校對相關字符集
4>如果沒有設定DB字符集和校對,使用server字符集和校對
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表字符集和校對
CREATE TABLE `t` (
`id` varchar(100) DEFAULT NULL,
`context` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8
創建table時指定,如果未設置根據db字符集
連接字符集
character_set_client 客戶端字符集
character_set_connection 連接字符集
character_ser_results 返回字符集
這3個參數確保相同,保證寫入讀出都正常
設置:
[mysql]
default-character-set=utf8
可統一設置:
Set names utf8
強制設置字符集
select _utf8 '字符集',_big5 '字符集'
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25583515/viewspace-2152015/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/25583515/viewspace-2152015/