【开发踩坑】生僻字插入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编码存在字段级别的设置,生僻字需要字段级别的编码支持

相关推荐
倔强的石头_10 小时前
《Kingbase护城河》——猎捕慢查询:执行计划的微观解析与索引调优实战
数据库
SelectDB11 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
jiayou642 天前
KingbaseES 表级与列级加密完全指南
数据库·后端
GBASE2 天前
G术时刻 |GBase 8s数据库事务并发控制之封锁技术介绍(下)
数据库
xiezhr3 天前
逛GitHub发现了一款免费的带AI功能的数据库管理工具
数据库·ai编程·dba
唐青枫4 天前
MySQL JSON 实战详解:从存储、查询、更新到 JSON_TABLE 与索引
sql·mysql
吃糖的小孩4 天前
给 QQ AI 机器人设计“可控记忆”:会话摘要、手动长期记忆与角色卡边界
数据库
小满8784 天前
5.Mysql事务隔离级别与锁机制
mysql
笃行3505 天前
金仓数据库数据安全双防线:静态存储加密与传输加密实战
数据库
笃行3505 天前
金仓数据库物理备份实战:sys_rman 全流程演练与误覆盖抢救
数据库