Mysql 如何使用 UTF8 字符集添加 varchar(21884) 列?

标签 mysql sql

如果我执行此查询:

CREATE TABLE `varchar_test1` (
  `id`  tinyint(1)  NOT NULL,
   `cloumn_1` varchar(21844) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;

没关系。

如果我执行此操作:

ALTER TABLE `varchar_test1` ADD COLUMN `cloumn_2` varchar(21844) NOT NULL;

我收到错误:

ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

如果我执行这个:

CREATE TABLE `varchar_test2` (
`id` int NOT NULL AUTO_INCREMENT,
`cloumn_1` varchar(21844) NOT NULL,
PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

我得到:

ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

为什么?

运行 mysql --version 返回

mysql Ver 14.14 Distrib 5.7.17, for macos10.12 (x86_64) using EditLine wrapper

最佳答案

您的问题是您的列存储多字节值,因此超出了最大行大小。

docs 中所述, MySQL 表的最大行大小为 65,535 字节,无论您使用什么引擎。添加两个 varchar(21844) 列意味着您超出了此限制。发生这种情况是因为表上的 CHARSETutf8(当前是 utf8mb3 的别名),因此 each character of a varchar in your table takes a maximum of 3 bytes (加上2个字节来存储长度)。对于一列 21,844 个字符来说没问题,但对于两列则不行。

要解决此问题,请使用 TEXT(或 TINYTEXTMEDIUMTEXT 等)而不是 VARCHAR 列。这将导致值单独存储,因此每一列实际上只会为行大小贡献几个字节。 (docs 中也对此进行了解释。)

另外,仅供引用:拼写是 column,而不是 cloumn

关于Mysql 如何使用 UTF8 字符集添加 varchar(21884) 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53737851/

相关文章:

mysql - 匹配系统

mysql - 1064错误无法解决

mysql - DBeaver:以毫秒显示日期时间

sql - 如何在 PostgreSQL 中使用正则表达式将列输入限制为字母数字

sql - 在每个分区中选择具有指定记录数的随机分区记录

mysql - 将分隔字符串拆分为存储过程中的变量

mysql - 根据 SQL 中的俱乐部成员对俱乐部进行排序

带标签的 MySQL 全文 bool 搜索

mysql - 连接多个表时如何计算不同值的数量?

sql - MySQL 是否允许在一张表上使用两个主键?