mysql error 1071: 创建唯一索引时字段长度限制的问题
作者:孤独王者¥ 时间:2024-01-12 16:32:20
一、先描述一下问题吧
如下创建表时候报错了
CREATE TABLE `xxx` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`sys_code` varchar(255) DEFAULT NULL COMMENT '系统编码',
`module_name` varchar(1000) DEFAULT NULL COMMENT '模块名',
`call_num` bigint(20) DEFAULT NULL COMMENT '调用次数',
`cost_dis` varchar(50) DEFAULT NULL COMMENT '耗时分布',
`date_time` varchar(50) DEFAULT NULL COMMENT '日期时间',
PRIMARY KEY (`id`),
UNIQUE KEY `sys_code` (`sys_code`, `module_name`, `cost_dis`, `date_time`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4
> 1071 - Specified key was too long; max key length is 3072 bytes
> 时间: 0s
二、显而易见
提示就是长度太长超过了mysql的最大配置。
三、问题和解决方案分析
1、首先考虑是否有方法将mysql这个长度限制调大
查了一下,如下操作:
(1)set global innodb_large_prefix=1;
mysql> show variables like 'innodb_large_prefix';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| innodb_large_prefix | ON |
+---------------------+-------+
1 row in set (0.00 sec)
(2)set global innodb_file_format=BARRACUDA;
mysql> show variables like 'innodb_file_format';
+--------------------+-----------+
| Variable_name | Value |
+--------------------+-----------+
| innodb_file_format | Barracuda |
+--------------------+-----------+
1 row in set (0.00 sec)
如果已经设置,还是创建不成功,只能考虑方案二
2、一般来说,唯一索引长度不应该太长
因为mysql会根据这个唯一索引做了一系列的操作,所以
(1)重新审核需求看看这样的唯一索引是否合理,看看能不能减少字段
(2)实在是不行,只能根据具体情况,将字段的长度调整小
比如,上面的例子就是没办法减少字段的,但是我又一定要建立唯一索引,最终,我选择了调整字段长度,将module_name的长度减少到了255,再次创建表,已成功。
来源:https://blog.csdn.net/qq_30966497/article/details/96316360
标签:mysql,error,1071,唯一,索引
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Cpython3.9源码解析python中的大小整数
2021-11-17 05:45:42
解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)
2023-11-17 08:13:30
Python pexpect模块及shell脚本except原理解析
2022-11-10 18:52:04
Python3搜索及替换文件中文本的方法
2023-08-24 04:33:44
Pytorch Tensor基本数学运算详解
2022-12-09 03:26:50
WEB页面工具语言XML支持的工具之运用
2008-05-29 10:55:00
linux下安装mysql简单的方法
2024-01-19 21:02:46
简单谈谈Python面向对象的相关知识
2022-08-25 19:11:23
OpenCV+Python3.5 简易手势识别的实现
2022-06-02 18:38:26
![](https://img.aspxhome.com/file/2023/4/107994_0s.jpg)
MySQL数据库中对前端和后台进行系统优化
2009-01-04 13:39:00
Pytorch反向求导更新网络参数的方法
2021-02-07 11:48:52
Python编程实现二分法和牛顿迭代法求平方根代码
2022-01-03 12:24:46
![](https://img.aspxhome.com/file/2023/7/112407_0s.png)
AJAX实现web页面中级联菜单的设计
2007-09-26 13:37:00
使用遗传算法求二元函数的最小值
2022-07-29 09:13:37
![](https://img.aspxhome.com/file/2023/5/93775_0s.jpg)
windows下mysql 8.0.12安装步骤及基本使用教程
2024-01-19 15:17:53
![](https://img.aspxhome.com/file/2023/6/119706_0s.jpg)
python路径的写法及目录的获取方式
2023-10-22 09:26:57
详解python--模拟轮盘抽奖游戏
2023-07-25 15:35:14
![](https://img.aspxhome.com/file/2023/5/71395_0s.png)
Python标准库defaultdict模块使用示例
2022-03-02 04:35:05
如何设计注册激活邮件
2010-01-12 13:14:00
![](https://img.aspxhome.com/file/UploadPic/20101/12/2010-01-03_163629-89s.png)
Javascript 虚拟 DOM详解
2024-04-18 10:31:30
![](https://img.aspxhome.com/file/2023/7/136557_0s.jpg)