MySQL:Specified key was too long; max key length is 767 bytes.
程序员文章站
2024-01-10 20:03:34
建表语句: 报错: 原因: 我这里使用 api,method 来做 api 表的联合主键,此时会根据这两个字段建立索引,charset 是 utf8 ,也就是一个字符3个字节, 那么总共索引的字节为: 500*3+50*3 = 1650 个字节,而mysql 要求的索引是 767 个字节。 解决: ......
建表语句:
create table if not exists api ( api varchar(500) not null , method varchar(50) not null default 'post', primary key (api,method) ) engine=innodb default charset=utf8 ;
报错:
specified key was too long; max key length is 767 bytes.
原因:
我这里使用 api,method 来做 api 表的联合主键,此时会根据这两个字段建立索引,charset 是 utf8 ,也就是一个字符3个字节, 那么总共索引的字节为: 500*3+50*3 = 1650 个字节,而mysql 要求的索引是 767 个字节。
解决:
将 api 字段改为 varchar(200), method 改为 varchar(20)
或者你可以通过限定字段的前n个字符为索引,参考: 来解决
unique key `uniq_store_code` (`record_date`,`store_code`(20),`sku_division_code`(20),`abc`(20))
表示后面store_code,sku_division_code,abc三个字段取前20字符作为唯一索引,这样的话就是长度就不会超出。
推荐阅读
-
MySQL:Specified key was too long; max key length is 767 bytes.
-
MySQL错误“Specified key was too long; max key length is 100
-
mysql建库时提示Specified key was too long max key length is 1000 bytes的问题的解决方法
-
MySQL错误“Specified key was too long; max key length is 1000 bytes”的解决办法
-
MySQL错误“Specified key was too long; max key length is 1000 bytes”的解决办法
-
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
-
peewee.InternalError: (1071, 'Specified key was too long; max key length is 1000 bytes')
-
Specified key was too long max key length is 1000 bytes
-
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
-
MySQL错误“Specified key was too long; max key length is 100