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>