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

Posted Silent1376

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL双字符主键过长 Specified key was too long; max key length is 767 bytes相关的知识,希望对你有一定的参考价值。

 

报错如图

 

建表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的主要内容,如果未能解决你的问题,请参考以下文章

PowerDesigner中表名过长,自动生成的主键名截取的问题

mysql设计规范

MySQL 数据库,主键为何不宜太长长长长长长长长?

MySQL 提示:MySql Specified key was too long; max key length is **** bytes

Oracle与mysql区别

数据库,主键为何不宜太长长长长长长长长?