mysql - 从mysql 7 导入到老版本的mysql6的报错:ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

访问量: 57

ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

对于一个string column, 如果对它进行了index的话,它的长度就不能是var char 255, 而是 < 190 的 var char

1. KEY `index_i......

例如,对于下面的建表代码(mysql 5.7下没问题, mysql 5.6就会报错)

DROP TABLE IF EXISTS `internal_messages`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `internal_messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) DEFAULT '0',
  `manager_id` int(11) DEFAULT NULL,
  `is_read` tinyint(1) DEFAULT '0',
  `title` varchar(255) DEFAULT NULL,
  `content` text,
  `is_release` tinyint(1) DEFAULT '0',
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `index_internal_messages_on_member_id` (`member_id`),
  KEY `index_internal_messages_on_manager_id` (`manager_id`),
  KEY `index_internal_messages_on_is_read` (`is_read`),
  KEY `index_internal_messages_on_title` (`title`),
  KEY `index_internal_messages_on_is_release` (`is_release`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

通过上面可以看到, 该表中,title是一个string, 长度是255, 这样在5.6导入的时候就会报错。

需要给他改成:   varchar(188)

2. 这个情况也会出问题:

DROP TABLE IF EXISTS `managers`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `managers` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(188) NOT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `role_id` smallint(5) unsigned NOT NULL DEFAULT '1',
  `encrypted_password` varchar(255) NOT NULL DEFAULT '',
  `reset_password_token` varchar(255) DEFAULT NULL,
  `reset_password_sent_at` datetime DEFAULT NULL,
  `remember_created_at` datetime DEFAULT NULL,
  `sign_in_count` int(11) unsigned NOT NULL DEFAULT '0',
  `current_sign_in_at` datetime DEFAULT NULL,
  `last_sign_in_at` datetime DEFAULT NULL,
  `current_sign_in_ip` varchar(255) NOT NULL DEFAULT '',
  `last_sign_in_ip` varchar(255) NOT NULL DEFAULT '',
  `token` varchar(255) DEFAULT NULL,
  `send_token_at` datetime DEFAULT NULL,
  `otp_secret` varchar(255) NOT NULL DEFAULT '',
  `is_otp_binded` tinyint(1) NOT NULL DEFAULT '0',
  `portrait_icon` varchar(255) DEFAULT NULL,
  `nick_name` varchar(255) DEFAULT NULL,
  `is_able_to_login` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否能登陆',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`),
  UNIQUE KEY `email` (`email`)    // 这里
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

UNIQUE KEY `email` (`email`) // 这里 ,也会出问题,所以要修改email的长度

修改后: `email` varchar(188) NOT NULL,

订阅/RSS Feed

Subscribe