mysql索引长度超过767bytes问题解决。Specified key was too long; max key length is 767 bytes

mysql索引长度超过767bytes问题解决。

 

错误信息:

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

MySQL的InnoDB存储引擎的表存在一系列的限制条件,其中比较常见的一种是表的字段索引长度限制,该限制与参数innodb_large_prefix相关。

原因分析:

导致上面报错的原因是由于InnoDB表的索引长度限制,在MySQL5.6版本后引入了参数innodb_large_prefix可以解决这个问题。该参数控制是否允许单列的索引长度超过767字节,有ON和OFF两个取值:

  1. :Innodb表的行记录格式是Dynamic或Compressed的前提下,单列索引长度上限扩展到3072个字节。
  2. :Innodb表的单例索引长度最多为767个字节,索引长度超出后,主键索引会创建失败,辅助索引会被截断成为前缀索引。

解决办法:

设置MySQL的全局参数innodb_large_prefix=ON,将InnoDB表的索引长度上限扩大到3072个字节。

MySQL5.6以及之前版本

  1. 以及之后的版本,不包括8.0,默认innodb_large_prefix为开启状态。
  2. 版本以及之后版本直接看到 4  ,5步骤

1. 查看MySQL数据库当前的配置;

show variables like 'innodb_large_prefix'; 
show variables like 'innodb_file_format';

 

 

2. 修改配置

set global innodb_large_prefix=ON;//开启不限制索引长度 
set global innodb_file_format=BARRACUDA;//这个不知道

 

3. 建表时需要指定ROW_FORMAT为dynamic或compressed

CREATE TABLE IMPORTS( 
`ID` int(10) NOT NULL AUTO_INCREMENT COMMENT '资产ID',
 PKG_ID int(10) NOT NULL COMMENT '资产ID',
 CLASS_NAME varchar(255) NOT NULL COMMENT '类名', 
) ENGINE=InnoDB ROW_FORMAT=dynamic DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

 

4. 表已经存在,只需修改。

ALTER TABLE `TableName ` row_format=dynamic;

 

5. 使用Navicat 15 可以直接设置行格式

右键表打开菜单--点击设计表--看到表设计窗口,点击选项--选择行格式--dynamic

 

现在就可以去添加索引了,

现在就可以去添加索引了~

现在就可以去添加索引了。

                               

MySQL5.6-摘自官方文档

innodb_large_prefix

Command-Line Format

--innodb-large-prefix[={OFF|ON}]

System Variable

innodb_large_prefix

Scope

Global

Dynamic

Yes

Type

Boolean

Default Value

OFF

Enable this option to allow index key prefixes longer than 767 bytes (up to 3072 bytes) for InnoDB tables that use DYNAMIC or COMPRESSED row format. (Creating such tables also requires the option values innodb_file_format=barracuda and innodb_file_per_table=true.) See Section 14.22, “InnoDB Limits” for maximums associated with index key prefixes under various settings.

For tables that use REDUNDANT or COMPACT row format, this option does not affect the permitted index key prefix length.

【机器翻译-仅供随便参考-切莫完全当真】

启用这个选项,对于使用动态或压缩行格式的InnoDB表,允许超过767字节(最多3072字节)的索引键前缀。

(创建这样的表还需要选项值innodb_file_format=barracuda 和 innodb_file_per_table=true。)

在不同的设置下,InnoDB限制与索引键前缀相关的最大值。

对于使用冗余或紧凑行格式的表,此选项不会影响允许的索引键前缀长度。

MySQL5.7-摘自官方文档

innodb_large_prefix

Command-Line Format

--innodb-large-prefix[={OFF|ON}]

Deprecated

Yes

System Variable

innodb_large_prefix

Scope

Global

Dynamic

Yes

Type

Boolean

Default Value

ON

When this option is enabled, index key prefixes longer than 767 bytes (up to 3072 bytes) are allowed for InnoDB tables that use DYNAMIC or COMPRESSED row format. See Section 14.23, “InnoDB Limits” for maximums associated with index key prefixes under various settings.

For tables that use REDUNDANT or COMPACT row format, this option does not affect the permitted index key prefix length.

innodb_large_prefix is enabled by default in MySQL 5.7. This change coincides with the default value change for innodb_file_format, which is set to Barracuda by default in MySQL 5.7. Together, these default value changes allow larger index key prefixes to be created when using DYNAMIC or COMPRESSED row format. If either option is set to a non-default value, index key prefixes larger than 767 bytes are silently truncated.

innodb_large_prefix is deprecated; expect it to be removed in a future release. innodb_large_prefix was introduced to disable large index key prefixes for compatibility with earlier versions of InnoDB that do not support large index key prefixes.

【机器翻译-仅供随便参考-切莫完全当真】

当启用该选项时,对于使用动态或压缩行格式的InnoDB表,允许超过767字节(最多3072字节)的索引键前缀。在不同的设置下,InnoDB限制与索引键前缀相关的最大值。

对于使用冗余或紧凑行格式的表,此选项不会影响允许的索引键前缀长度。

innodb_large_prefix在MySQL 5.7中默认开启。这个变化与innodb_file_format的默认值变化一致,MySQL 5.7默认设置为Barracuda。

在使用动态或压缩行格式时,这些默认值的更改允许创建更大的索引键前缀。

如果任一选项设置为非默认值,大于767字节的索引键前缀将被静默截断。

innodb_large_prefix弃用;希望它在未来的版本中被删除。innodb_large_prefix的引入是为了禁用大索引键前缀,以兼容早期版本的InnoDB不支持大索引键前缀。

参考以及摘取的文档:

https://blog.csdn.net/jian876601394/article/details/94313071

https://blog.csdn.net/baidu_27446515/article/details/106397298

  • 11
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes 。 引用:【[Err] 1071 - Specified key was too longmax key length is 767 bytes,【各版本mysql均已解决】】http://t.csdn.*** was too longmax key length is 767 bytes 二、分析: 。 问题:请告诉我,sql错误1071Specified key was too long; max key length is 767 bytes SQL错误1071 "Specified key was too long; max key length is 767 bytes" 是由于在MySQL数据库中创建或修改表时,指定的键的长度超过了最大限制引起的错误。最大键长度限制在767字节。这个问题可以在各个版本的MySQL解决。 为了解决这个问题,你可以采取以下几种方法之一: 1. 缩短键的长度: 可以通过减少键的长度解决这个问题。你可以检查表的每个键,并尝试减少键的长度,使其不超过767字节。 2. 使用不同的字符集和排序规则: 有时候,如果你在创建表时使用了特定的字符集和排序规则,会导致键的长度变长。你可以尝试使用不同的字符集和排序规则,以确保键的长度在限制范围内。 3. 使用前缀索引: 如果键的长度依然超过限制,你可以考虑使用前缀索引。前缀索引只使用键的一部分作为索引,从而减少了键的长度。 4. 升级MySQL版本: 如果你使用的是较旧的MySQL版本,你可以尝试升级到最新版本。最新版本的MySQL通常会解决一些旧版本中存在的限制和问题。 综上所述,SQL错误1071 "Specified key was too long; max key length is 767 bytes" 是由于键的长度超过MySQL的最大限制引起的。你可以通过缩短键的长度、使用不同的字符集和排序规则、使用前缀索引或升级MySQL版本来解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [导入sql文件报错:1071 Specified key was too long; max key length is 767 bytes](https://blog.csdn.net/weixin_39841572/article/details/113251789)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【数据库】[Err] 1071 - Specified key was too longmax key length is 767 bytes](https://blog.csdn.net/weixin_53791978/article/details/128857775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值