CREATE TABLE test_1 ( Fid bigint(20) unsigned NOT NULL, Ftype tinyint(4) unsigned NOT NULL, Flist varchar(65532) DEFAULT NULL, Fstatus tinyint(3) unsigned DEFAULT '0', Ftime bigint unsigned DEFAULT '0', Faddtime bigint unsigned DEFAULT '0', PRIMARY KEY (Fid,Ftype) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 建表报以下错误: ERROR 1074 (42000): Column length too big for column 'Flist' (max = 21845); use BLOB or TEXT instead
虽然知道mysql建表的时候列长度有65535的限制,可是一直没有时间整理这个问题,接着今天整理一下。mysql
ERROR 1074 (42000): Column length too big for column 'Flist' (max = 21845); use BLOB or TEXT instead
从上面的报错咱们知道Flist列指定值不能大于21845 字节(mysql官方手册中定义,建立表的字段长度限制为65535 bytes,这个是指全部列指定的长度和,固然不包括TEXT和BLOB类型的字段)。
还有一点咱们须要注意的是咱们定义列长度时指定的长度单位为字符,上面提到的65535限制的单位为字节。不一样字符集下每一个字符占用的字节数不一样,utf8下每一个字符占用3个字节(65535/3=21845),gbk下每一个字符占用2个字节(65535/2=32767),latin1字符集下一个字符占用一个字节。sql
<span style="color:#333333;">实验1: CREATE TABLE test_1 ( Flist varchar(21845) DEFAULT NULL ) ENGINE=InnoDB 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 </span><span style="color:#ff0000;">##咱们发现给表指定一个列, 列长度为21845字符(utf8下最大长度限制),可是建表依然报错。这是由于还有别的一些开销, 因此咱们不能指定列长度为最大限制21845(测试发现指定长度为21844后建表成功)</span> <span style="color:#333333;">实验2: CREATE TABLE test_1 ( Fid bigint(20) unsigned NOT NULL, Ftype tinyint(4) unsigned NOT NULL, Flist varchar(21844) DEFAULT NULL, Fstatus tinyint(3) unsigned DEFAULT '0', Ftime bigint unsigned DEFAULT '0', Faddtime bigint unsigned DEFAULT '0', PRIMARY KEY (Fid,Ftype) ) ENGINE=InnoDB 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 </span><span style="color:#ff0000;">##虽然Flist 长度指定为21844,可是由于还 有其余非TEXT和BLOB字段存在,因此报错。这是由于65535长度限制是针对表中全部列的长 度和的最大限制,若是列的长度和超过该值,建表依然会报错。(除了TEXT和BLOB类型的字 段的)</span>