【MySQL】双字符主键过长 Specified key was too long; max key length is 767 bytes

2022/3/8 19:15:26

本文主要是介绍【MySQL】双字符主键过长 Specified key was too long; max key length is 767 bytes,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

MySQL版本:

 

这个情况在 8.0.28版本没有出现

 

报错如图

 

建表SQL:

DROP TABLE IF EXISTS `pt_dict_common`;
CREATE TABLE `pt_dict_common`  (
  `DMBH` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '代码编号',
  `DMMC` varchar(250) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '代码名称',
  `DMLB` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '代码类别',
  `DMLBMC` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '代码类别名称',
  `SJDMBH` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '上级代码编号',
  `TIMELINE` datetime(0) NULL DEFAULT NULL COMMENT '时间戳',
  `SORT` bigint(20) NULL DEFAULT NULL COMMENT '排序',
  PRIMARY KEY (`DMBH`, `DMLB`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '通用字典' ROW_FORMAT = Dynamic;

 

问题排查定位在 声明的双主键上面:

DROP TABLE IF EXISTS `pt_dict_common`;
CREATE TABLE `pt_dict_common`  (
  `DMBH` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '代码编号',
  `DMMC` varchar(250) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '代码名称',
  `DMLB` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '代码类别',
  `DMLBMC` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '代码类别名称',
  `SJDMBH` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '上级代码编号',
  `TIMELINE` datetime(0) NULL DEFAULT NULL COMMENT '时间戳',
  `SORT` bigint(20) NULL DEFAULT NULL COMMENT '排序',
  PRIMARY KEY (`DMBH`, `DMLB`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '通用字典' ROW_FORMAT = Dynamic;

默认字符双主键使用B+TREE

 

猜测是DMBH字段长度过大,在多次尝试后发现,长度设定为 191 可以创建

但是原因不知道是什么

 



这篇关于【MySQL】双字符主键过长 Specified key was too long; max key length is 767 bytes的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程