Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

CREATE TABLE wp_locations (
        `id` INT(11) NOT NULL AUTO_INCREMENT,
        `city` VARCHAR(255) NOT NULL,
        `name` VARCHAR(255) NOT NULL,
        CONSTRAINT `city_name` UNIQUE (`city`, `name`)
    ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

I got an sql error '#1071 - Specified key was too long; max key length is 767 bytes'

What am I doing wrong?

like image 278
Paul Roefs Avatar asked Nov 17 '16 16:11

Paul Roefs


People also ask

What is an SQL error?

SQL keyword errors occur when one of the words that the SQL query language reserves for its commands and clauses is misspelled. For example, writing “UPDTE” instead of “UPDATE” will produce this type of error.

What causes SQL error?

The cause of these problems can vary from file system corruption, underlying hardware system issues, driver issues, corrupted pages in memory, or problems with the SQL Server Engine.

What does SQLCODE =- 206 mean?

The name of the object that could not be resolved. Possible reasons for this error include: The specified name is not a column of any of the source or target tables or views of the statement.


2 Answers

MySQL always reserves the max amount for a UTF8 field which is 4 bytes so with 255 + 255 with your DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; you are over the 767 max key length limit.

You can only reduce the single varchar length or don't use a composite key.

like image 63
ScaisEdge Avatar answered Sep 20 '22 16:09

ScaisEdge


I had the same exact issue. I've added these lines to a new conifg file in /etc/my.conf.d directory named umb4-support.cnf

[mysqld]
innodb_large_prefix=true
innodb_file_format=barracuda
innodb_file_per_table=true

After restarting the maria db service, my import scripts ran without this issue. Godspeed!

like image 25
Ivan Loreto Avatar answered Sep 21 '22 16:09

Ivan Loreto