#1071 - 指定的密钥太长了MySQL

时间:2018-03-28 22:19:30

标签: mysql

我真的很难将.sql文件上传到phpmyadmin。

我得到的错误是......

SQL查询:

CREATE TABLE `coupons` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `code` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` varchar(300) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `percentage` decimal(5,2) NOT NULL DEFAULT '0.00',
  `active_from` datetime DEFAULT '2018-03-23 12:15:55',
  `active_to` datetime DEFAULT '2018-03-30 12:15:55',
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `coupons_code_unique` (`code`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
MySQL说:

  

#1071 - 指定的密钥太长;最大密钥长度为767字节

这是我认为它所指的原始部分。

-- Table structure for table `coupons`
--

DROP TABLE IF EXISTS `coupons`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `coupons` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `code` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` varchar(300) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `percentage` decimal(5,2) NOT NULL DEFAULT '0.00',
  `active_from` datetime DEFAULT '2018-03-23 12:15:55',
  `active_to` datetime DEFAULT '2018-03-30 12:15:55',
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `coupons_code_unique` (`code`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `coupons`

1 个答案:

答案 0 :(得分:1)

Utfmb4每个字符使用4个字节。 4×255高于最大密钥长度。 将代码字段从varchar(255)更改为较小的长度。最好使用实际长度和char数据类型。

请记住。更长的密钥需要昂贵的计算。如果您有更大的文本,请创建另一个字段,您可以在其中保存较大字符串的计算散列。