持续学习&持续更新中…
学习态度:守破离
【宋红康 MySQL数据库 】【高级篇】【02】MySQL字符集、比较规则、大小写规范、sql_mode
默认字符集
- MySQL8.0版本之前,默认字符集为
latin1 ,utf8 字符集指向的是utf8mb3 。 - 在数据库设计的时候往往需要将默认的字符集编码修改为utf8字符集,如果忘记了修改默认的字符集,就会出现乱码问题
- 从MySQL8.0开始,数据库的默认编码为
utf8mb4 ,从而避免了上述乱码问题。
MySQL8
mysql> show variables like '%character%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.01 sec)
mysql> create database dbtest01;
Query OK, 1 row affected (0.01 sec)
mysql> use dbtest01;
Database changed
mysql> create table emp01(id int, name varchar(15));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into emp01(id, name) values(10, 'Tom');
Query OK, 1 row affected (0.03 sec)
mysql> insert into emp01(id, name) values(12, '数据库');
Query OK, 1 row affected (0.00 sec)
mysql> select * from emp01;
+------+-----------+
| id | name |
+------+-----------+
| 10 | Tom |
| 12 | 数据库 |
+------+-----------+
2 rows in set (0.00 sec)
mysql> show create table emp01;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
| emp01 | CREATE TABLE `emp01` (
`id` int DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create database dbtest01;
+----------+------------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------------------------------------------------------------------------+
| dbtest01 | CREATE DATABASE `dbtest01` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MySQL5
MySQL5默认的客户端和服务器都使用latin1 ,不支持中文。
mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
mysql> create database dbtest01;
Query OK, 1 row affected (0.00 sec)
mysql> use dbtest01;
Database changed
mysql> create table emp01(id int, name varchar(15));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into emp01(id, name) values(10, 'Tom');
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp01(id, name) values(12, '数据库');
ERROR 1366 (HY000): Incorrect string value: '\xE6\x95\xB0\xE6\x8D\xAE...' for column 'name' at row 1
mysql> show create table emp01;
+-------+------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------+
| emp01 | CREATE TABLE `emp01` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create database dbtest01;
+----------+---------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------+
| dbtest01 | CREATE DATABASE `dbtest01` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+---------------------------------------------------------------------+
1 row in set (0.00 sec)
修改字符集
修改MySQL5的默认字符集
vi /etc/my.cnf
在[mysqld]下添加:
character_set_server=utf8
重启MySQL服务:
systemctl restart mysqld
现在MySQL5的字符集就是:
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)
之后再创建新的数据库、新的表,在不指定字符集的情况下,就会使用默认的utf8编码
修改已有的数据库、表的字符集
虽然修改了MySQL5的默认字符集,但是,之前创建的已有的数据库、表的字符集是不会改变的。
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.01 sec)
mysql> use dbtest01;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show create database dbtest01;
+----------+---------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------+
| dbtest01 | CREATE DATABASE `dbtest01` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+---------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table emp01;
+-------+------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------+
| emp01 | CREATE TABLE `emp01` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
使用如下命令修改数据库字符集:
alter database 数据库名 character set 'utf8';
mysql> use dbtest01;
Database changed
mysql> alter database dbtest01 character set 'utf8';
Query OK, 1 row affected (0.01 sec)
mysql> show create database dbtest01;
+----------+-------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------+
| dbtest01 | CREATE DATABASE `dbtest01` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
修改表字符集:
alter table 表名 convert to character set 'utf8';
mysql> alter table emp01 convert to character set 'utf8';
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> show create table emp01;
+-------+----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------+
| emp01 | CREATE TABLE `emp01` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into emp01(id, name) values(33, '数据库');
Query OK, 1 row affected (0.00 sec)
mysql> select * from emp01;
+------+-----------+
| id | name |
+------+-----------+
| 10 | Tom |
| 33 | 数据库 |
+------+-----------+
2 rows in set (0.00 sec)
字符集和比较规则
MySQL有4个级别的字符集和比较规则,分别是:
- 服务器级别(MySQL server 级别)
- 数据库级别
- 表级别
- 列级别
执行:show variables like 'character%';
mysql> show variables like 'character%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.01 sec)
执行:show character set; 以查看MySQL支持的字符集:
mysql> show character set;
+----------+---------------------------------+---------------------+--------+
| 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 |
| utf16le | UTF-16LE Unicode | utf16le_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 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.00 sec)
utf8与utf8mb4:
比较规则
常用操作
utf8、utf8mb4一般使用以_general_ci、_unicode_ci为后缀的比较规则:
服务器级别
数据库级别
如果创建和修改数据库时没有指定字符集和比较规则,那么将使用服务器级别的字符集和比较规则作为数据库的字符集和比较规则。
表级别
列级别
小结
- 如果
创建或修改数据库 时没有显示的指定字符集和比较规则,则该数据库默认用服务器 的字符集和比较规则 - 如果
创建或修改表 时没有显示的指定字符集和比较规则,则该表默认用所在数据库 的字符集和比较规则 - 如果
创建或修改列(字段) 时没有显示的指定字符集和比较规则,则该列默认用所在表 的字符集和比较规则
请求到响应过程中字符集的变化
不同操作系统的默认值可能不同:
SQL大小写规范
默认规范
Linux下大小写规则修改
SQL编写建议
sql_mode的合理设置
介绍
宽松模式、严格模式
查看sql_mode
举例:
mysql> select @@global.sql_mode;
+-----------------------------------------------------------------------------------------------------------------------+
| @@global.sql_mode |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
设置sql_mode
sql_mode可设置多个
举例:
设置sql_mode为宽松模式:
SET SESSION sql_mode = '';
sql_mode解释
参考
尚硅谷宋红康: MySQL数据库(入门到高级,菜鸟到大牛).
本文完,感谢您的关注支持!
|