字符集
1.什么是字符集
#字符集:是一个系统支持的所有抽象字符的集合。字符是各种文字和符号的总称,包括各国家文字、标点符号、图形符号、数字等。
最早的字符集:ASCII码
中国的字符集:gbk,utf8,gbk2312
日本字符集:shift-JIS
韩国字符集:Euc-kr
万国编码:Unicode字符集
#常用的字符集:
gbk:一个汉字占用2个字节
utf8:一个汉字占用3个字节
utf8mb4:一个汉字占用4个字节
#字符集修改要求:
包含关系才可以修改
#查看字符集:
mysql> show charset;
2.校验规则
#查看校验规则
mysql> show collation;
| latin7_general_ci | latin7 |
| latin7_general_cs | latin7 |
| latin7_bin | latin7 |
#校验规则区别
1.以ci结尾:不区分大小写
2.以cs或者bin结尾:区分大小写
#大小写不同,相同字段也不能添加
mysql> alter table city add nAME varchar(10);
ERROR 1060 (42S21): Duplicate column name ‘nAME‘
3.统一字符集
#1.xshell字符集
#2.linux系统字符集
#临时修改
[root@db03 ~]# LANG=zh_CN.UTF-8
#永久修改
[root@db03 ~]# vim /etc/sysconfig/i18n #Centos6
[root@db03 ~]# vim /etc/locale.conf #Centos7
LANG="zh_CN.UTF-8"
#3.数据库字符集
1)cmake 字符集指定
cmake .
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all 2)配置文件
[root@db03 ~]# vim /etc/my.cnf
[mysqld]
character-set-server=utf8
#4.创建数据库时指定字符集
mysql> create database db7 charset utf8;
Query OK, 1 row affected (0.00 sec)
mysql> show create database db7;
+----------+--------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------+
| db7 | CREATE DATABASE `db7` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)
#5.建表时根据库的字符集来建表
mysql> create table tb1(id int);
#6.指定字符集建表
mysql> create table tb3(id int) charset utf8mb4;
Query OK, 0 rows affected (0.01 sec)
mysql> show create table tb3;
+-------+------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------+
| tb3 | CREATE TABLE `tb3` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-------+------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
#7.修改库的字符集
mysql> show create database db7;
+----------+--------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------+
| db7 | CREATE DATABASE `db7` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter database db7 charset gbk;
Query OK, 1 row affected (0.00 sec)
mysql> show create database db7;
+----------+-------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------+
| db7 | CREATE DATABASE `db7` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+-------------------------------------------------------------+
1 row in set (0.00 sec)
#8.修改表的字符集
mysql> show create table tb2;
+-------+---------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------+
| tb2 | CREATE TABLE `tb2` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table tb2 charset gbk;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table tb2;
+-------+--------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------+
| tb2 | CREATE TABLE `tb2` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+--------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MySQL字符集