Specified key was too long; max key length is 1000 bytes

作者

使用mariadb创建数据表,遇到了这个是提示(Specified key was too long; max key length is 1000 bytes)。 原因是索引键太长了,超过了1000,出现这样的情况,调整一下索引的字段的长度就可以。下面说下遇到的具体情况。

创建一个数据表

create table if not exists `options`(
    `option_id` bigint(20) unsigned not null auto_increment,
    `option_name` varchar(251) not null default '',
    `option_value` longtext not null,
    `autoload` varchar(20) not null default 'no',
    primary key (`option_id`),
    unique key (`option_name`)
) engine=myisam  default charset=utf8mb4;

如果直接执行出现的错误是 (Specified key was too long; max key length is 1000 bytes)

出现的原因

  1. 索引过长超过的 1000
  2. 数据表引擎myisam的限制

修改的方式有两种,任何一种都可以修改

  1. 删除索引 unique key (option_name)
  2. 修改索引的长度

说明1

索引的长度限制是1000, 因为 utf8mb4一个字符是占用4个字节,所以修改的数要小 (1000/4 =250)

说明2

Specified key was too long; max key length is 767 bytes
如果遇到了可能用的是 innodb 的引擎

回复

您的电子邮箱地址不会被公开。