语法:
ALTER TABLE <table_name> DEFAULT CHARSET=<new_charset>;
示例:将 utf8 转成 utf8mb4
建表:
use test;
CREATE TABLE `user_info` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8;
插入数据:
insert into user_info (name) values('乐天');
修改字符编码:
ALTER TABLE user_info DEFAULT CHARSET=utf8mb4;
查看建表语句:
mysql> show create table user_info
+-----------+----------------------------------------------------------+
| Table | Create Table |
+-----------+----------------------------------------------------------+
| user_info | CREATE TABLE `user_info` ( |
| | `id` bigint(20) NOT NULL AUTO_INCREMENT, |
| | `name` varchar(45) CHARACTER SET utf8 NOT NULL, |
| | PRIMARY KEY (`id`) |
| | ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 |
+-----------+----------------------------------------------------------+
问题
- 如果原本是 utf8mb4 ,文本里有表情符号,转成 utf8 会发生什么?
- gbk 到 utf8mb4 会发生什么?