[数据库/MYSQL]#解决缺陷#设置Unique索引时:"[Err] 1071 - Specified key was too long; max key length is 767 bytes"
1 问题复现
- 原表结构:
CREATE TABLE `XX_TEMPERATURE` (
`FLOW_ID` int(11) NOT NULL COMMENT '独立的数据表或FTP唯一标识', -- 与上述其它表的字段完全无关联,属自创ID
`TABLE_FLOW_ID` varchar(512) COLLATE utf8_bin DEFAULT NULL COMMENT '表,元数据uniqueID', -- 本次博客的重点
-- MYSQL_127.0.0.1_3306_root_CJ_TESTDBDatabase^CJ_TEST|DBTable^Person
`TEMPERATURE_VALUE` int(11) NOT NULL COMMENT '(表级)热度值', -- 应保证其值实时更新
PRIMARY KEY (`FLOW_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='热度值表';
- 现需求
对 TABLE_FLOW_ID 新增 Unique 索引
- 正常做法:
ALTER TABLE `XX_TEMPERATURE` ADD CONSTRAINT UNIQUE_INDEX_FOR_TABLE_FLOW_ID_OF_TEMPERATURE_TABLE UNIQUE(`TABLE_FLOW_ID`);
然鹅,出现了如下错误:
[SQL]ALTER TABLE `XX_TEMPERATURE` ADD CONSTRAINT UNIQUE_INDEX_FOR_TABLE_FLOW_ID UNIQUE(`TABLE_FLOW_ID`);
[Err] 1071 - Specified key was too long; max key length is 767 bytes
2 解决方案
本质上,就是 VARCHAR(512) 512>256了,导致的失败。(更深层原因待深究)
方案1: 缩短Unique索引字段的长度
ALTER TABLE `XX_TEMPERATURE` MODIFY `TABLE_FLOW_ID` VARCHAR(255);
方案2: [配置] 系统变量 innodb_large_prefix / 系统变量 innodb_file_format / 表配置项 ROW_FORMAT
要解决 [Err] 1071 - Specified key was too long; max key length is 767 bytes这一缺陷,必须满足如下3个条件:
系统变量 innodb_large_prefix = ON
系统变量 innodb_file_format = Barracuda
表配置项 ROW_FORMAT = DYNAMIC / COMPRESSED
- step1 配置 系统变量 innodb_large_prefix
set global innodb_large_prefix=on;
相关知识延伸
-- 系统变量 innodb_large_prefix : 不同版本,开闭情况不同 (MySQL 5.6.41和5.6.33 默认关闭 ; MySQL 5.7 默认开启)
---- 如果启用了系统变量innodb_large_prefix,则:对于使用DYNAMIC或COMPRESSED行格式的InnoDB表,索引键前缀限制为3072字节。
---- 如果禁用innodb_large_prefix,则:对于任何行格式的表,索引键前缀限制为767字节。
-- 查看 innodb_large_prefix
show variables like '%innodb_large_prefix%';
-- 设置 innodb_large_prefix -- on / off
set global innodb_large_prefix=off; -- off
- step2 配置 系统变量 innodb_file_format
set global innodb_file_format=Barracuda;
相关知识延伸
-- 查看
show variables like '%innodb_file_format%';-- innodb_file_format = Antelope / innodb_file_format_check = ON / innodb_file_format_max = Barracuda
-- 设置
set global innodb_file_format=Antelope;
- step3 配置 表配置项 ROW_FORMAT
ALTER TABLE XX_TEMPERATUREROW_FORMAT = DYNAMIC;
相关知识延伸
-- 备注
---- https://www.cnblogs.com/wilburxu/p/9435818.html
---- 只有在MYSAM 的数据库引擎才支持属性: FIXED
---- fixed--->dynamic: 这会导致CHAR变成VARCHAR
---- dynamic--->fixed: 这会导致VARCHAR变成CHAR
-- 查看
SHOW TABLE STATUS LIKE "%XX_TEMPERATURE%"; -- ROW_FORMAT 字段 : Compact [ DEFAULT / Compact / FIXED / DYNAMIC / COMPRESSED / ... ]
-- 设置
ALTER TABLE XX_TEMPERATUREROW_FORMAT = Compact;
X 参考资料
- mysql5.6------ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes - CSDN
- MySQL InnoDB 行记录格式(ROW_FORMAT) - 博客园
[数据库/MYSQL]#解决缺陷#设置Unique索引时:"[Err] 1071 - Specified key was too long; max key length is 767 bytes"的更多相关文章
- mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry 'xxx' for key 'PRIMARY'
mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry 'xxx' for key 'PRIMARY' 解决方法: 在my.cnf的[mysql ...
- 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的字段设置 ...
- hive异常:创建MySQL时Specified key was too long; max key length is 1000 bytes
2015-11-13 14:44:44,681 ERROR [main]: DataNucleus.Datastore (Log4JLogger.java:error(115)) - An excep ...
- Hive集成Mysql作为元数据时,提示错误:Specified key was too long; max key length is 767 bytes
在进行Hive集成Mysql作为元数据过程中.做全然部安装配置工作后.进入到hive模式,运行show databases.运行正常,接着运行show tables:时却报错. 关键错误信息例如以下: ...
- 关于MySQL字符集问题:Specified key was too long; max key length is 767 bytes
[文章来源]http://blog.csdn.net/cindy9902/article/details/6215769 MySQL: ERROR 1071 (42000): Specified ke ...
- Mysql Specified key was too long; max key length is 767 bytes
今天导入一个数据库时,看到以下报错信息: Specified key was too bytes 直译就是索引键太长,最大为767字节. 查看sql库表文件,发现有一列定义如下: 列 名:cont ...
- EF MySQL 提示 Specified key was too long; max key length is 767 bytes错误
在用EF的CodeFirst操作MySql时,提示 Specified key was too long; max key length is 767 bytes错误,但数据库和表也建成功了.有高人知 ...
- Mysql: Specified key was too long; max key length is 1000 bytes
在使用quartz持久化的时候,笔者使用的mysql,为了以后方便迁移数据,笔者的Mysql默认引擎MyISAM 于是顺理成章的执行了quartz-2.2.3\docs\dbTables\tables ...
- 执行nova-manage db sync时出错,提示’Specified key was too long; max key length is 1000 bytes’
执行nova-manage db sync时出错: 2012-03-24 14:07:01 CRITICAL nova [-] (OperationalError) (1071, ‘Specified ...
- 在hive执行创建表的命令,遇到异常com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too long; max key length is 767 bytes
今天在练习hive的操作时,在创建数据表时,遇到了异常 FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.ex ...
随机推荐
- Tomcat和Maven安装与配置
链接:https://pan.baidu.com/s/1aezz2pfCn0DCCPw8udQFXA 提取码:wd4f 一.网站发布1.1.为什么要用tomcat网页开发好了,该如何发布呢?我们需要一 ...
- train_data
for images, labels in train_data: for images, labels in train_data: img = images[0] img = img.numpy( ...
- drf(3)
1 不使用drf编写5个接口 1.1 路由 urlpatterns = [ path('books/', views.BookView.as_view()), path('books/&l ...
- ubuntu安装matplotlib失败:Can't rollback pillow, nothing uninstalled.
今天在ubuntu1804上面使用pip安装matplotlib,安装失败,报错如下: ---------------------------------------- Can't rollback ...
- kafka 学习
https://kafka.apache.org/quickstart C:\W_O_R_K\kafka_2.12-2.2.0\kafka_2.12-2.2.0\bin\windows\zookeep ...
- 备份linux系统日志脚本
#!/bin/bash#script_name bkup_log.sh#7 0 * * 1 cd /home/tools/;./bkup_log.sh >& /dev/nullproce ...
- 单向链表&有关类和对象
// Test515.cpp: 定义控制台应用程序的入口点.// #include "stdafx.h"#include <iostream>using namespa ...
- egret 图片跨域
//图片跨域 egret.ImageLoader.crossOrigin = "anonymous";
- django+ajax实现xlsx文件下载功能
前端代码 $("#id_pullout").click(function () { //发送ajax请求 $.ajax({ url: '/pullout/', //请求的url m ...
- 对使用网站模板编写自己的jsp页面的收获
收获与问题 先感叹一句模板的强大,模板只要按照我的上一篇博客的步骤进行修改,我们就能拥有一个自己写好看许多的界面了. 我目前套用模板还不是很熟练,套用的速度还需要加快,不过目前的我,已经掌握了基本要领 ...