今天有開發反應他的建表語句錯誤,我看了下,提示:
MySQL Error 1170 (42000): BLOB/TEXT Column Used in Key Specification Without a Key Length
原因是:
MySQL不允許在BLOB/TEXT,TINYBLOB, MEDIUMBLOB, LONGBLOB, TINYTEXT, MEDIUMTEXT, LONGTEXT,VARCHAR建索引,因為前面那些列類型都是可變長的,MySQL無法保證列的唯一性,只能在BLOB/TEXT前n個字節上建索引,這個n最大多長呢?做個測試:
root@test 03:53:58>create table lingluo_1 ( -> id int(20) not null auto_increment, -> aaa text, -> primary key(id), -> index idx_aaa(aaa(399)) -> ) -> COLLATE='gbk_chinese_ci' -> ENGINE=InnoDB; Query OK, 0 rows affected, 1 warning (0.00 sec) root@test 03:54:58>show warnings; +---------+------+---------------------------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------------------------+ | Warning | 1071 | Specified key was too long; max key length is 767 bytes | +---------+------+---------------------------------------------------------+ 1 row in set (0.00 sec) root@test 03:55:05>select 767/2; +----------+ | 767/2 | +----------+ | 383.5000 | +----------+ 1 row in set (0.00 sec) root@test 03:55:47>create table lingluo_2 ( -> id int(20) not null auto_increment, -> aaa text, -> primary key(id), -> index idx_aaa(aaa(383)) -> ) -> COLLATE='gbk_chinese_ci' -> ENGINE=InnoDB; Query OK, 0 rows affected (0.02 sec) root@test 03:55:53>create table lingluo_3 ( -> id int(20) not null auto_increment, -> aaa text, -> primary key(id), -> index idx_aaa(aaa(383)) -> ) -> charset=utf8 -> ENGINE=InnoDB; Query OK, 0 rows affected, 1 warning (0.00 sec) root@test 03:58:08>show warnings; +---------+------+---------------------------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------------------------+ | Warning | 1071 | Specified key was too long; max key length is 767 bytes | +---------+------+---------------------------------------------------------+ 1 row in set (0.00 sec) root@test 03:58:17>select 767/3; +----------+ | 767/3 | +----------+ | 255.6667 | +----------+ 1 row in set (0.00 sec) root@test 03:58:27>create table lingluo_4 ( -> id int(20) not null auto_increment, -> aaa text, -> primary key(id), -> index idx_aaa(aaa(255)) -> ) -> charset=utf8 -> ENGINE=InnoDB; Query OK, 0 rows affected (0.02 sec) root@test 03:59:04>create table lingluo_5 ( -> id int(20) not null auto_increment, -> aaa text, -> primary key(id), -> index idx_aaa(aaa(256)) -> ) -> charset=utf8 -> ENGINE=InnoDB; Query OK, 0 rows affected, 1 warning (0.01 sec) root@test 03:59:17> root@test 03:59:17>show warnings; +---------+------+---------------------------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------------------------+ | Warning | 1071 | Specified key was too long; max key length is 767 bytes | +---------+------+---------------------------------------------------------+ 1 row in set (0.00 sec)
對於gbk(一個漢字占兩個字節)編碼的字段,只能前383個字符建索引;對於utf8(一個漢字占三個字節)編碼的字段,只能前255個字符建索引;對於latin編碼的字段,只能前767個字符建索引;
root@test 03:59:22>create table lingluo_6 ( -> id int(20) not null auto_increment, -> aaa text, -> primary key(id), -> index idx_aaa(aaa(768)) -> ) -> charset=latin1 -> ENGINE=InnoDB; Query OK, 0 rows affected, 1 warning (0.01 sec) root@test 04:02:08>show warnings; +---------+------+---------------------------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------------------------+ | Warning | 1071 | Specified key was too long; max key length is 767 bytes | +---------+------+---------------------------------------------------------+ 1 row in set (0.00 sec) root@test 04:02:15>create table lingluo_7 ( -> id int(20) not null auto_increment, -> aaa text, -> primary key(id), -> index idx_aaa(aaa(767)) -> ) -> charset=latin1 -> ENGINE=InnoDB; Query OK, 0 rows affected (0.01 sec) root@test 04:32:39>create table lingluo_8 ( id int(20) not null auto_increment, aaa varchar(10000), primary key(id), index idx_aaa(aaa) ) charset=latin1 ENGINE=InnoDB; Query OK, 0 rows affected, 1 warning (0.01 sec) root@test 04:32:46>show warnings; +---------+------+---------------------------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------------------------+ | Warning | 1071 | Specified key was too long; max key length is 767 bytes | +---------+------+---------------------------------------------------------+ 1 row in set (0.00 sec)
同樣的,當一個表里原來有非TEXT或者非BLOB字段(這些字段上有唯一索引或者普通索引)變為BLOB或TEXT的時候,也會遇到標題上的錯誤,如:
root@test 04:44:15>create table lingluo_10 ( -> id int(20) not null auto_increment, -> aaa varchar(383), -> primary key(id), -> index idx_aaa(aaa) -> ) -> charset=gbk -> ENGINE=InnoDB; Query OK, 0 rows affected (0.01 sec) root@test 04:44:39>alter table lingluo_10 modify aaa text; ERROR 1170 (42000): BLOB/TEXT column 'aaa' used in key specification without a key length