0

I'm really struggling with uploading a .sql file to phpmyadmin.

The error I get is this...

SQL query:

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 said:

#1071 - Specified key was too long; max key length is 767 bytes

Here is the original section in which I think it's referring to.

-- 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`
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • 1
    Possible duplicate of [#1071 - Specified key was too long; max key length is 767 bytes](https://stackoverflow.com/questions/1814532/1071-specified-key-was-too-long-max-key-length-is-767-bytes) – Ken Y-N Mar 29 '18 at 02:24

1 Answers1

1

Utfmb4 uses 4 byte per character. 4 × 255 is above max key length. Change your code field from varchar (255) to a smaller length. Preferably use a realistic length and char data type.

Keep in mind. Longer keys require expensive computation. If in case you have larger text, create another field where you can save a computed hash of the larger string.

Krish
  • 5,917
  • 2
  • 14
  • 35
  • Excellent trick. Do a simple (and fast) MD5 hash of the combined indexes, then use those when searching – Stephen R Jun 25 '18 at 22:19