操作MySQL出错提示“BLOB/TEXT column request_data in key specification without a key length”解决办法 mysql添加字段索引失败 BLOB/TEXT column 'col2' used in key specification without a key length

错误原因:

       查阅资料后才知道,原来Mysql数据库对于BLOB/TEXT这样类型的数据结构只能索引前N个字符。所以这样的数据类型不能作为主键,也不能是UNIQUE的。所以要换成VARCHAR,但是VARCHAR类型的大小也不能大于255,当VARCHAR类型的字段大小如果大于255的时候也会转换成小的TEXT来处理。所以也同样有问题。

 解决办法:

看了下该表的数据结构发现request_data字段类型是text

查询了下发现是:MySQL只能将BLOB/TEXT类型字段设置索引为BLOB/TEXT数据的钱N个字符.索引指定下request_data的长度就可以了 :

alter table foo add index request_data (request_data(10))

联合索引

alter table testcase add unique index(request_url,request_data(100));

 执行这条sql前先删除表中所有重数据,删除sql如下:

DELETE testcase
FROM
 testcase, 
 (
  SELECT
  max(id) id,
  api_purpose,
request_url,
  request_data,
assert_method,
check_point,
correlation,
active,
creater 
  FROM
   testcase
  GROUP BY
  api_purpose,
request_url,
  request_data,
assert_method,
check_point,
correlation,
active,
creater
  HAVING
   count(*) > 1
 ) t2
WHERE
 testcase.api_purpose = t2.api_purpose 
 and testcase.api_purpose = t2.api_purpose 
 and testcase.request_url = t2.request_url 
 and testcase.request_data = t2.request_data 
 and testcase.assert_method = t2.assert_method 
 and testcase.check_point = t2.check_point 
 and testcase.correlation = t2.correlation 
 and testcase.active = t2.active 
 and testcase.creater = t2.creater 
 and testcase.id <t2.id;

 

参考文章:

这篇错误原因讲的很清晰

操作MySQL出错提示“BLOB/TEXT column used in key specification without a key length”解决办法

这篇是解决方案:

mysql添加字段索引失败 BLOB/TEXT column 'col2' used in key specification without a key length

猜你喜欢

转载自www.cnblogs.com/kaerxifa/p/11965981.html