如果我执行此查询:
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)
列意味着您超出了此限制。发生这种情况是因为表上的 CHARSET
是 utf8
(当前是 utf8mb3
的别名),因此 each character of a varchar
in your table takes a maximum of 3 bytes (加上2个字节来存储长度)。对于一列 21,844 个字符来说没问题,但对于两列则不行。
要解决此问题,请使用 TEXT
(或 TINYTEXT
、MEDIUMTEXT
等)而不是 VARCHAR
列。这将导致值单独存储,因此每一列实际上只会为行大小贡献几个字节。 (docs 中也对此进行了解释。)
另外,仅供引用:拼写是 column
,而不是 cloumn
。
关于Mysql 如何使用 UTF8 字符集添加 varchar(21884) 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53737851/