MySQL的字符集支持(Character Set Support)有 两个方面



1、字符集(Character set)

2、排序比较方式(Collation)。

通过命令:show character set 和 show collation 可以分别查看Mysql支持的字符集和排序方式。

mysql> show character set;

mysql> show collation;

Mysql对于字符集的支持细化到四个层次:

1、服务器(server);

2、数据库(database);

3、数据表(table)(字段column);

4、连接(connection);

1.MySQL默认字符集

     

  

   

   

   

   

   

   

     

2.查看默认字符集(默认情况下,Mysql的字符集是latin1(ISO_8859_1)

   

     mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+---------------------------------+
| Variable_name            | Value                           |
+--------------------------+---------------------------------+
| character_set_client     | latin1                          |
| character_set_connection | latin1                          |
| character_set_database   | latin1                          |
| character_set_filesystem | binary                    |
| character_set_results    | latin1                          |
| character_set_server     | latin1                          |
| character_set_system    | utf8                            |
| character_sets_dir       | D:\Program Files\MySQL\MySQL Server 5.1\share\charsets\ |
+--------------------------+---------------------------------+

mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+

3.修改默认字符集

(1) 最简单的修改方法,就是修改mysql的my.ini文件中的字符集键值,如 :

    default-character-set = utf8
     

 

   使用 mysql> SHOW VARIABLES LIKE 'character%';查看,发现数据库编码均已改成utf8
+--------------------------+---------------------------------+
| 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       | D:\Program Files\MySQL\MySQL Server 5.1\share\charsets\ |
+--------------------------+---------------------------------+

 

   mysql> SET character_set_client = utf8 ;
     mysql> SET character_set_connection = utf8 ;
     mysql> SET character_set_database = utf8 ;
     mysql> SET character_set_results = utf8 ;
    

    mysql> SET collation_connection = utf8 ;
     mysql> SET collation_database = utf8 ;
    

       

SET NAMES 'utf8';

它相当于下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;

总结:

1) 正确的设定数据库编码.MySQL4.0以下版本的字符集总是默认ISO8859-1,MySQL4.1在安装的时候会让你选择。如果你准备使用UTF- 8,那么在创建数据库的时候就要指定好UTF-8(创建好以后也可以改,4.1以上版本还可以单独指定表的字符集)。

2) 正确的设定数据库connection编码.设置好数据库的编码后,在连接数据库时候,应该指定connection的编码,比如使用jdbc连接时,指定连接为utf8方式.