美文网首页
MySQL客户端报错1071 - Specified key w

MySQL客户端报错1071 - Specified key w

作者: 蜗牛在北京 | 来源:发表于2019-04-04 14:27 被阅读0次

    创建表时,报如下错误:

    1071 - Specified key was too long; max key length is 767 bytes

    意思就是索引过长,超过了索引的最大长度限制767字节。

    具体参考:https://blog.csdn.net/ljfphp/article/details/80406907

    需要对设置的index进行筛选,

    这个767 bytes是什么玩意?

    答:

    (1)在mysql 5.5.3之前,mysql的InnoDB引擎,要求设置的主键长度不得超过767bytes。

    mysql的MyIsam引擎的主键长度不得超过1000 bytes。

    (2)在mysql中,gbk字符集会占用2个字节。utf8字符会占用3个字节。

    而且从mysql5.5.3之后的版本,mysql 开始支持utf8m4字符,代表着一个字符占用4个字节。

    也就是说:

    (255+10+10)*3 = 825  //在用utf8作为字符集的时候,超过了规定的767 bytes

    (255+10+10)*2 = 550  //当该用gbk作为字符集的时候

    (255+10+10)*4 = 1100  //当用utf8m4作为字符集的时候,也超标了

    一个国外的帖子:

    链接:

    https://stackoverflow.com/questions/1814532/1071-specified-key-was-too-long-max-key-length-is-767-bytes

    原文:

    767 bytes is the stated prefix limitation for InnoDB tables in MySQL version 5.6 (and prior versions). It's 1,000 bytes long for MyISAM tables. In MySQL version 5.7 and upwards this limit has been increased to 3072 bytes.

    查看自己的数据库版本,5.6.38. 

    有两种方案:

    1.升级mysql到5.7以上

    2.修改索引,降低索引长度。若实在无法满足业务需要,还是需要升级。

    相关文章

      网友评论

          本文标题:MySQL客户端报错1071 - Specified key w

          本文链接:https://www.haomeiwen.com/subject/pphfiqtx.html