When I executed the following command:
ALTER TABLE `mytable` ADD UNIQUE (
`column1` ,
`column2`
);
I got this error message:
#1071 - Specified key was too long; max key length is 767 bytes
Information about column1 and column2:
column1 varchar(20) utf8_general_ci
column2 varchar(500) utf8_general_ci
I think varchar(20)
only requires 21 bytes while varchar(500)
only requires 501 bytes. So the total bytes are 522, less than 767. So why did I get the error message?
#1071 - Specified key was too long; max key length is 767 bytes
Here is my original answer:
However, it doesn't work for all the cases.
It is actually a problem of using indexes on VARCHAR columns with the character set
utf8
(orutf8mb4
), with VARCHAR columns that have more than a certain length of characters. In the case ofutf8mb4
, that certain length is 191.Please refer to the Long Index section in this article for more information how to use long indexes in MySQL database: http://hanoian.com/content/index.php/24-automate-the-converting-a-mysql-database-character-set-to-utf8mb4
you could add an column of the md5 of long columns
The answer about why you get error message was already answered by many users here. My answer is about how to fix and use it as it be.
Refer from this link.
use my_database_name;
set global innodb_large_prefix=on;
set global innodb_file_format=Barracuda;
The problem of this fix is if you export db to another server (for example from localhost to real host) and you cannot use MySQL command line in that server. You cannot make it work there.
change your collation. You can use utf8_general_ci that supports almost all
Just changing
utf8mb4
toutf8
when creating tables solved my problem. For example:CREATE TABLE ... DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
toCREATE TABLE ... DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
.I did some search on this topic finally got some custom change
For MySQL workbench 6.3.7 Version Graphical inter phase is available
For Versions below 6.3.7 direct options are not available so need to go with command prompt