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` 
1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent trick. Do a simple (and fast) MD5 hash of the combined indexes, then use those when searching

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.