mysql字符集

一、什么是字符集。

1、 字符(Character)是各种文字和符号的总称,包括各国家文字、标点符号、图形符号、数字等。

2、 字符集(Character set)是多个字符的集合,字符集种类较多,每个字符集包含的字符个数不同,常见字符集名称:ASCII字符集、GB2312字符集、BIG5字符集、 GB18030字符集、Unicode字符集等。

3、 计算机要准确的处理各种字符集文字,就需要进行字符编码,以便计算机能够识别和存储各种文字。中文文字数目大,而且还分为简体中文繁体中文两种不同书写规则的文字,而计算机最初是按英语单字节字符设计的,因此,对中文字符进行编码,是中文信息交流的技术基础。

注:字符编码,是编码字符集和实际存储数值之间的转换关系。字符,是根据字符编码方案转换为一个二进制数值存储在计算机中的。所以,字符编码是定义在字符集上的映射规则。

4、 百度详解:https://jingyan.baidu.com/article/7f41ecec214710593d095cd9.html

二、查看字符集

1.查看MYSQL数据库服务器和数据库字符集

1.1.show variables like '%character%';

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_client转换character_set_filesystem, 默认binary是不做任何转换的。                                                       |
| character_set_results    | utf8   --数据库给客户端返回时使用的编码格式,如果没有指明,使用服务器默认的编码格式。                                                       |
| character_set_server     | utf8  --服务器安装时指定的默认编码格式,这个变量建议由系统自己管理,不要人为定义。                                                        |
| character_set_system     | utf8 --数据库系统使用的编码格式,这个值一直是utf8,不需要设置,它是为存储系统元数据的编码格式。                                                         |
| character_sets_dir       | C:\Program Files (x86)\MySQL\MySQL Server 5.5\share\charsets\   --这个变量是字符集安装的目录。 |
+--------------------------+---------------------------------------------------------------+
8 rows in set (0.00 sec)

  

2.查看MYSQL所支持的字符集。

show charset;

mysql> show charset;
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
| dec8     | DEC West European           | dec8_swedish_ci     |      1 |
| cp850    | DOS West European           | cp850_general_ci    |      1 |
| hp8      | HP West European            | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                    | ascii_general_ci    |      1 |
| ujis     | EUC-JP Japanese             | ujis_japanese_ci    |      3 |
| sjis     | Shift-JIS Japanese          | sjis_japanese_ci    |      2 |
| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 |
| tis620   | TIS620 Thai                 | tis620_thai_ci      |      1 |
| euckr    | EUC-KR Korean               | euckr_korean_ci     |      2 |
| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 |
| gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   |      2 |
| greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 |
| cp1250   | Windows Central European    | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |
| ucs2     | UCS-2 Unicode               | ucs2_general_ci     |      2 |
| cp866    | DOS Russian                 | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  |      1 |
| macce    | Mac Central European        | macce_general_ci    |      1 |
| macroman | Mac West European           | macroman_general_ci |      1 |
| cp852    | DOS Central European        | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic          | latin7_general_ci   |      1 |
| utf8mb4  | UTF-8 Unicode               | utf8mb4_general_ci  |      4 |
| cp1251   | Windows Cyrillic            | cp1251_general_ci   |      1 |
| utf16    | UTF-16 Unicode              | utf16_general_ci    |      4 |
| cp1256   | Windows Arabic              | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic              | cp1257_general_ci   |      1 |
| utf32    | UTF-32 Unicode              | utf32_general_ci    |      4 |
| binary   | Binary pseudo charset       | binary              |      1 |
| geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |
| cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   |      2 |
| eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci |      3 |
+----------+-----------------------------+---------------------+--------+
39 rows in set (0.00 sec)

注:MySQL在5.5.3之后增加了这个utf8mb4的编码。

  utf8最多用到3个字节

  utf8mb4最多可以用到4字节

  utf8mb4存放的字符集包含utf8

3.查看库的字符集。

语法:show create database  库名\G;

mysql> show create database xscj_\G;
*************************** 1. row ***************************
       Database: xscj_
Create Database: CREATE DATABASE `xscj_` /*!40100 DEFAULT CHARACTER SET utf8 */
1 row in set (0.01 sec)

4.查看表的字符集。

语法:show table status from 库名 like  "表名";

 

mysql>  show table status from xscj_ like 'xs';
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation       | Checksum | Create_options | Comment |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| xs   | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |            0 |   6291456 |           NULL | 2021-04-08 15:08:24 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
1 row in set (0.02 sec)

 

5.查看表中所有列的字符集。

语法:show full columns from 表名;

 

mysql> show full columns from xs;
+----------+------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| Field    | Type       | Collation       | Null | Key | Default | Extra | Privileges                      | Comment |
+----------+------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| 学号        | char(6)    | utf8_general_ci | NO   | PRI | NULL    |       | select,insert,update,references |         |
| 姓名         | char(8)    | utf8_general_ci | NO   |     | NULL    |       | select,insert,update,references |         |
| 专业名       | char(10)   | utf8_general_ci | YES  |     | NULL    |       | select,insert,update,references |         |
| 性别        | tinyint(1) | NULL            | NO   |     | 1       |       | select,insert,update,references |         |
| 出生日期         | date       | NULL            | NO   |     | NULL    |       | select,insert,update,references |         |
| 总学分        | tinyint(1) | NULL            | YES  |     | NULL    |       | select,insert,update,references |         |
| 照片        | blob       | NULL            | YES  |     | NULL    |       | select,insert,update,references |         |
| 备注        | text       | utf8_general_ci | YES  |     | NULL    |       | select,insert,update,references |         |
+----------+------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
8 rows in set (0.01 sec)

 

注:如果乱码,通过set names 'xxx'; 把字符集设置的与客户端所用的一样。 set names 'GBK';

 

二、设置字符集

1.创建时指定字符集

1.1创建库的时候指定字符集:

  语法:create database 库名 default character set=字符集;

mysql>  create database xscj__ default character set=utf8;
Query OK, 1 row affected (0.00 sec)

1.2创建表的时候指定字符集:

  语法:create table 表名(属性)default character set = 字符集;

mysql> create table xss(a char(2) not null primary key)default character set=utf8mb4;
Query OK, 0 rows affected (0.02 sec)

 

2.创建后指定字符集

  2.1修改库的字符集

 语法:alter database 库名 default character set 字符集;

mysql> alter database xscj__ default character set gbk;
Query OK, 1 row affected (0.00 sec)

 

  2.2修改表的字符集

  语法:alter table 表名 convert to character set 字符集;

mysql> alter table xs convert to character set utf8;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

  2.3.修改字段的字符集

  语法:alter table 表名 modify 字段名 字段属性 character set 字符集;

mysql> alter table xs modify 备注 text character set utf8mb4;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

 

  2.4修改全局字符集

 

/*建立连接使用的编码*/
set character_set_connection=utf8;
/*数据库的编码*/
set character_set_database=utf8;
/*结果集的编码*/
set character_set_results=utf8;
/*数据库服务器的编码*/
set character_set_server=utf8;

set character_set_system=utf8;

set collation_connection=utf8;

set collation_database=utf8;

set collation_server=utf8;

 

注:set指令只是临时生效,永久生效到mysql的安装目录下my.ini / my.ncf文件,修改里面对应的配置信息即可。linux版本是my.cnf,win下的是my.ini

3.my.ncf配置文件中修改

 

上一篇:SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ‘已消毒‘ for column ‘sfxd‘ at row 1


下一篇:Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operatio