root@localhost : test 10:30:54>CREATE TABLE tb_test (
-> recordid varchar(32) NOT NULL,
-> areaShow varchar(10000) DEFAULT NULL,
-> areaShow1 varchar(10000) DEFAULT NULL,
-> areaShow2 varchar(10000) DEFAULT NULL,
-> PRIMARY KEY (recordid)
-> ) 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. You have to change some columns to TEXT or BLOBs
报错
root@localhost : test 10:31:01>CREATE TABLE tb_test (
-> recordid varchar(32) NOT NULL,
-> areaShow varchar(30000) DEFAULT NULL,
-> areaShow1 varchar(30000) DEFAULT NULL,
-> areaShow2 varchar(30000) DEFAULT NULL,
-> PRIMARY KEY (recordid)
-> ) ENGINE=INNODB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected, 3 warnings (0.26 sec)
可以建立,只是类型被转换了。
root@localhost : test 10:31:14>show warnings;
+-------+------+----------------------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------------------+
| Note | 1246 | Converting column 'areaShow' from VARCHAR to TEXT |
| Note | 1246 | Converting column 'areaShow1' from VARCHAR to TEXT |
| Note | 1246 | Converting column 'areaShow2' from VARCHAR to TEXT |
+-------+------+----------------------------------------------------+
3 rows in set (0.00 sec)
疑问:
为什么字段小(10000)的反而报错,而大(30000)的则可以建立。为什么小的不能直接转换呢?
解决:
这里多感谢orczhou的帮助,原来MySQL在建表的时候有个限制:MySQL要求一个行的定义长度不能超过65535。具体的原因可以看:
http://dev.mysql.com/doc/refman/5.1/en/silent-column-changes.html
(1)单个字段如果大于65535,则转换为TEXT 。
(2)单行最大限制为65535,这里不包括TEXT、BLOB。
按照上面总结的限制,来解释出现的现象:
第一个情况是:
单个字段长度:varchar(10000) ,字节数:10000*3(utf8)+(1 or 2) = 30000 ,小于65535,可以建立。
单行记录长度:varchar(10000)*3,字节数:30000*3(utf8)+(1 or 2) = 90000,大于65535,不能建立,所以报错:
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs
第二个情况是:
单个字段长度:varchar(30000) ,字节数:30000*3+(1 or 2) = 90000 , 大于65535,需要转换成TEXT,才可以建立。所以报warnings。
单行记录长度:varchar(30000)*3,因为每个字段都被转换成了TEXT,而TEXT没有限制,所以可以建立表。
root@localhost : test 10:31:14>show warnings;
+-------+------+----------------------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------------------+
| Note | 1246 | Converting column 'areaShow' from VARCHAR to TEXT |
| Note | 1246 | Converting column 'areaShow1' from VARCHAR to TEXT |
| Note | 1246 | Converting column 'areaShow2' from VARCHAR to TEXT |
+-------+------+----------------------------------------------------+