【开发踩坑】生僻字插入MySQL失败

背景

生产环境插入数据报错:

bash 复制代码
java.sql.SQLException: Incorrect string value: '\xF0\xAC\xB1\x96' for column 'answer' at row 1

设置answer字段值为 "𬱖"出现错误

生僻字设置出错;

排查

编码

查看库表属性:

SQL 复制代码
ENGINE InnoDB AUTO_INCREMENT =1479001 DEFAULT CHARSET utf8 ROW_FORMAT DYNAMIO
utf8

10.1.10.6 The utf8mb4 Character Set (4-Byte UTF-8 Unicode Encoding)

The character set named utf8 uses a maximum of three bytes per character and contains only BMP characters. As of MySQL 5.5.3, the utf8mb4 character set uses a maximum of four bytes per character supports supplemental characters:

For a BMP character, utf8 and utf8mb4 have identical storage characteristics: same code values, same encoding, same length.

For a supplementary character, utf8 cannot store the character at all, while utf8mb4 requires four bytes to store it. Since utf8 cannot store the character at all, you do not have any supplementary characters in utf8 columns and you need not worry about converting characters or losing data when upgrading utf8 data from older versions of MySQL.

utf8mb4 is a superset of utf8.

总而言之,utf-8 只支持 3 个字节的字符;

如果是超过 4 个字节,那么就需要使用 utf8mb4 (max byte 4 ):加强版的 utf8 来存储

生僻字

查看生僻字占用的字节:

4 字节,显然 utf8 是不满足的

解决

提交 sql:

SQL 复制代码
alter table qt_check_answer default  CHARACTER SET utf8mb4

alt 之后还是报错;

查看库表:

表级别的设置更新了,但是字段还没有更新;

复现一下:



结论

此时更新字段级别的编码类型:解决

结论:MySQL编码存在字段级别的设置,生僻字需要字段级别的编码支持

相关推荐
我是黄骨鱼7 分钟前
【零基础学数据库|第二篇】MySql启动!!!
数据库·mysql
陌上丨9 分钟前
什么是Redis的大Key和热Key?项目中一般是怎么解决的?
数据库·redis·缓存
Remember_99310 分钟前
Spring 事务深度解析:实现方式、隔离级别与传播机制全攻略
java·开发语言·数据库·后端·spring·leetcode·oracle
小园子的小菜10 分钟前
深入剖析HBase HFile原理:文件结构、Block协作与缓存机制
数据库·缓存·hbase
空空kkk15 分钟前
SSM项目练习——hami音乐(三)
java·数据库
好奇的菜鸟21 分钟前
Ubuntu 18.04 启用root账户图形界面登录指南
数据库·ubuntu·postgresql
天桥下的卖艺者23 分钟前
使用R语言编写一个生成金字塔图形的函数
开发语言·数据库·r语言
Facechat35 分钟前
鸿蒙开发入坑篇(九):本地数据库 (RDB) 深度解析
数据库·华为·harmonyos
Dxy123931021636 分钟前
MySQL删除表语句详解
数据库·mysql
uoKent1 小时前
MySQL常见命令梳理大纲
数据库·mysql