数据库
首页 > 数据库> > 【MySQL】双字符主键过长 Specified key was too long; max key length is 767 bytes

【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 可以创建

但是原因不知道是什么

 

标签:COMMENT,ci,SET,utf8mb4,max,COLLATE,key,NULL,主键
来源: https://www.cnblogs.com/mindzone/p/15979236.html