SQL Create Table Error - Key is too long? - mysql

I am using the following SQL command to create a table, but it's giving the me the following error.
#1071 - Specified key was too long; max key length is 1000 bytes
I check SQL documentation, and it says that "Message: Specified key was too long; max key length is %d bytes" which didn't quite help.
CREATE TABLE Consist_of
(
dssn INTEGER,
pre_no VARCHAR(255),
pname VARCHAR(255),
trade_name VARCHAR(255),
FOREIGN KEY (dssn, pre_no) REFERENCES Prescription(dssn, pre_no),
FOREIGN KEY (pname, trade_name) REFERENCES Drug(pname, trade_name)
);

So as per the error it is clear that you are passing the maximum limit. For MyISAM tables it is 1000 bytes and for InnoDB it is set to 767 bytes.
You can refer to the resolution of this bug reported.

With a character set of (I assume) utf8, a VARCHAR(255) would need 255 x 3 = 765 bytes for its index.
In your case (255+255+255) x 3 = 2295 (for each varchar row).
You can increase the maximum InnoDB index prefix size in MySQL 5.6 to 3072 bytes by setting innodb_large_prefix to YES along with other settings that you'll also need in order to enable that one, discussed here:
http://dev.mysql.com/doc/refman/5.6/en/innodb-parameters.html#sysvar_innodb_large_prefix

This error means that length of index index is more then 1000 bytes. MySQL and storage engines may have this restriction. I have got similar error on MySQL 5.5 - 'Specified key was too long; max key length is 3072 bytes' when ran this script:
CREATE TABLE IF NOT EXISTS example_table1 (
first_col varchar(500) NOT NULL,
second_col varchar(500) NOT NULL,
third_col varchar(500) NOT NULL,
KEY `index` (first_col, second_col, third_col)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
UTF8 is multi-bytes, and key length is calculated in this way - 500 * 3 * 3 = 4500 bytes.
But note, next query works!
CREATE TABLE IF NOT EXISTS example_table1 (
first_col varchar(500) NOT NULL,
second_col varchar(500) NOT NULL,
third_col varchar(500) NOT NULL,
KEY `index` (first_col, second_col, third_col)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Related

Error: Specified key was too long; max key length is 1000 bytes - importing database into MySQL

I have a new wampserver installation on a windows 10 machine and I am trying to import a previously existing database containing around 10 different tables into a new database via phpmyadmin.
When I run a 'create table' command for a specific table in the sql query editor I receive the following error:
#1071 - Specified key was too long; max key length is 1000 bytes
This is the command that I am running:
DROP TABLE IF EXISTS `subscription_items`;
CREATE TABLE IF NOT EXISTS `subscription_items` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`subscription_id` bigint(20) UNSIGNED NOT NULL,
`stripe_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`stripe_plan` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`quantity` int(11) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `subscription_items_subscription_id_stripe_plan_unique` (`subscription_id`,`stripe_plan`) USING HASH,
KEY `subscription_items_stripe_id_index` (`stripe_id`(250))
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Any ideas as to which setting could be changed to enable this error to disappear? I have tried looking in the my.ini file but cannot spot the setting to alter. Also this database worked in a previous older installation of wamp.
No setting can change this limit of MyISAM tables.
https://dev.mysql.com/doc/refman/8.0/en/myisam-storage-engine.html says:
The maximum key length is 1000 bytes. This can also be changed by changing the source and recompiling.
Your indexes columns are 8 bytes for the bigint, plus 255 * 4 bytes per character for the string. For indexing purposes, it has to assume you might use the full length of the utf8mb4 string in some row, even if you haven't done so yet. That totals 1028 bytes.
You could fix this by using varchar(248) or shorter.
You can also fix this by using InnoDB instead of MyISAM. InnoDB supports up to 3072 bytes for an index length in recent versions of MySQL.
I recommend using InnoDB instead of MyISAM regardless.

How to resolve "specified key was too long max key length is 255 bytes" in mysql?

Whenever i fire this query from one of the mysql client (emma):
CREATE TABLE `tbl_mappings` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`private_id` int(11) unsigned NOT NULL,
`name` tinytext NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`private_id`,`name`(255)),
KEY `FK_tbl__private_integrations_mappings_tbl__private_integrations` (`private_id`),
CONSTRAINT `FK_tbl__private_integrations_mappings_tbl__private_integrations` FOREIGN KEY (`private_id`) REFERENCES `tbl__private_integrations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
i get error : specified key was too long max key length is 255 bytes
i am using mysql server 5.7,ubuntu 16.04
And i have tried adding configuration in my.cnf under [mysqld] :
innodb_file_format=barracuda
innodb_file_per_table=1
innodb_large_prefix=1
init_connect='SET collation_connection = utf8mb4_unicode_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default-storage-engine=InnoDB
And then restarted mysql service .still it wont work.
Any help is appreciated.
Thank you.
EDIT
Issue appears to be related to the TINYTEXT datatype. (I can replicate the observed behavior with MySQL version 5.7.17-0ubuntu0.16.04.1-log, using either InnoDB or MyISAM.)
The short answer (as a workaround, how to resolve the 1071 warning) is to use datatype VARCHAR(255) in place of TINYTEXT.
I ran several test cases with various character sets (utf8, utf8mb4, latin1) and using InnoDB and MyISAM storage engines. The 1071 warning appears to be related to the prefix length specified in the index on the TINYTEXT column... appears to be a MySQL limit on the prefix length (not specifically related to InnoDB, since I can replicate the behavior with MyISAM.) I did not test with any other TEXT types other than TINYTEXT.
PREVIOUS ANSWER
Index key length limit for InnoDB tables is 767 bytes.
The name(255) in the key definition is specifying the first 255 characters of name. With the MySQL utf8 characterset, a character can take from one to three bytes. And 255 times three is 765. Add in the four bytes for the int private_id, and that's 769, which exceeds the maximum.
That's why you are getting the error.
Several approaches to resolving that.
Easiest would be to reduce the number of characters of name that are included in the index, e.g.
UNIQUE KEY `name` (`private_id`,`name`(254))
If that doesn't satisfy your use case, then you might need to consider using the deprecated innodb_large_prefix setting. You would need to use DYNAMIC or COMPRESSED row format. See the discussions here:
https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html
https://dev.mysql.com/doc/refman/5.7/en/innodb-row-format-specification.html
There are 5 solutions here .
If you are hitting the limit because of trying to use CHARACTER SET utf8mb4. Then do one of the following (each has a drawback) to avoid the error:
⚈ Upgrade to 5.7.7 for 3072 byte limit -- your cloud may not provide this;
⚈ Change 255 to 191 on the VARCHAR -- you lose any keys longer than 191 characters (unlikely?);
⚈ ALTER .. CONVERT TO utf8 -- you lose Emoji and some of Chinese;
⚈ Use a "prefix" index -- you lose some of the performance benefits.
⚈ Stay with 5.6/5.5/10.1 but perform 4 steps to raise the limit to 3072 bytes:
SET GLOBAL innodb_file_format=Barracuda;
SET GLOBAL innodb_file_per_table=1;
SET GLOBAL innodb_large_prefix=1;
logout & login (to get the global values);
ALTER TABLE tbl ROW_FORMAT=DYNAMIC; (or COMPRESSED)
On second glance, don't use TINYTEXT, change to VARCHAR(255) which does not need the prefixing!
On third glance, UNIQUE(x, y(255)) is very likely to be wrong. It says "the combination of x and part of y is unique". It does not say x and all of y is unique.
Fourth... Which version of 5.7? Works fine with 5.7.15:
mysql> CREATE TABLE `tbl_mappings` (
-> `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-> `private_id` int(11) unsigned NOT NULL,
-> `name` tinytext NOT NULL,
-> PRIMARY KEY (`id`),
-> UNIQUE KEY `name` (`private_id`,`name`(255)),
-> KEY `private_id` (`private_id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8
-> ;
Query OK, 0 rows affected (0.03 sec)
mysql> select ##version;
+-----------+
| ##version |
+-----------+
| 5.7.15 |
+-----------+
1 row in set (0.00 sec)
mysql> SHOW CREATE TABLE tbl_mappings\G
*************************** 1. row ***************************
Table: tbl_mappings
Create Table: CREATE TABLE `tbl_mappings` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`private_id` int(11) unsigned NOT NULL,
`name` tinytext NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`private_id`,`name`(255)),
KEY `private_id` (`private_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

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

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?
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.
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!

Specified key was too long; max key length is 767 bytes

I understand the InnoDB index max length is 767 bytes.
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(254) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
.....
`token` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`rank` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_token_index` (`token`),
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
I want to create a index on my email.
alter table agent add UNIQUE index idx_on_email (email);
But got the error message:
Specified key was too long; max key length is 767 bytes.
But the length of token column only 128 bytes, email is 254 bytes, not above 767 bytes. Hope anyone can help me! Thanks in advance!
varchar(254) when you use utf8mb4, means 254 character and each character has 4 bytes, the email field requires at least 1016 bytes (254 * 4).
you may look at this article:
http://wildlyinaccurate.com/mysql-specified-key-was-too-long-max-key-length-is-767-bytes/
so you can make your email column: varchar(100)
An alternate option would be to reassess the nature and the constraints of the data stored in that table, and how they relate to other data in JOINs, then justify, or not, that a charset and collation of utf8mb4 is needed.
Example: if the data stored and/or compared to other will never have special characters longer then 2 bytes, you may just replace charset and collation with utf8 and utf8_general_ci respectively (or alternate). You may go even shorter for ascii ones.
This assessment / justifying job is a good practice anyway, and may bring accrued performance for free.

SQL key length error makes no sense

The following looks perfectly reasonable to me:
CREATE TABLE `mydb`.`Temp` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`x` VARCHAR ( 300 ) NOT NULL ,
`id_foo` INT NULL DEFAULT NULL,
FOREIGN KEY ( `id_foo`) REFERENCES `Foo` (`id`) ON DELETE CASCADE ,
INDEX (`id_foo`),
INDEX (`x`),
UNIQUE (`id_foo`, `x`)
) ENGINE = INNODB;
With MySQL this gives an error
#1071 - Specified key was too long; max key length is 767 bytes
This seems wrong because the whole row is 309 bytes: less than 767, not even half. What's going on?
According to the MYSQL Documantation : http://dev.mysql.com/doc/refman/5.0/en/create-index.html
MySQL has different limits on the amount of space you can use to define indexes on column(s)
for MyISAM it's 1,000 bytes;
for InnoDB it's 767 .
Moreover, the data type of those columns matters - for VARCHAR, it's 3x
So, an index on a VARCHAR(300) just like in your table will take 900 of those bytes which is greater than 767 bytes, max key length.
EDIT: Apparently this is not a bug of MySQL, but the UTF8 in MySQL that supports up to 3 bytes. Also, with introducing 4-byte utf8 character set (WL#1213) maximum possible key length changed from 255 to 191 characters (191 * 4 + 2 = 766 where 2 bytes hold for the length). All -utf, -utf8mb4, -utf16, -utf32 are affected from this change beginning with the MySQL version 5.5 or higher.
Try determining how long that index needs to be in order to remain effective:
SELECT COUNT(DISTINCT(`x`)) as n_unique,
COUNT(DISTINCT(LEFT(`x`,200))) as n_100,
COUNT(DISTINCT(LEFT(`x`,150))) as n_150,
COUNT(DISTINCT(LEFT(`x`,100))) as n_100,
COUNT(DISTINCT(LEFT(`x`,50))) as n_50,
COUNT(DISTINCT(LEFT(`x`,25))) as n_25,
COUNT(DISTINCT(LEFT(`x`,10))) as n_10
FROM Temp;
Dividing each n_ result by the n_unique will give you the percent coverage. Once you have that you can likely get decent coverage with a smaller number of characters.
ALTER TABLE Temp ADD index x_improved(20)
Where 20 is really the n_ count of distinct variables given above.
It's your INDEX (x) that's the problem. It works if the VARCHAR is shorter.
What character set are you using?
Based on that error, it appears you are using a multi-byte character set, probably utf8, which reserves 3 bytes per character. So a varchar(300) in utf8 results in a 900 byte key length, which exceeds the innodb limit of 767.
In order to create your table with those indexes, you either need to a different character set, or shorten the length of your column. If you just had the index on x I would recommend simply indexing the first 255 characters of that column, but given your unique index that include x that solution is not viable, since it would reject values as duplicates if they match on the first 255 characters, even if they differ in the last 45 characters.
Here are a couple of example that will work:
-- shorten x to 255 characters
CREATE TABLE `mydb`.`Temp` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`x` VARCHAR ( 255 ) NOT NULL ,
`id_foo` INT NULL DEFAULT NULL,
FOREIGN KEY ( `id_foo`) REFERENCES `Foo` (`id`) ON DELETE CASCADE ,
INDEX (`id_foo`),
INDEX (`x`),
UNIQUE (`id_foo`, `x`)
) ENGINE = INNODB;
-- use single-byte character set
CREATE TABLE `mydb`.`Temp` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`x` VARCHAR ( 300 ) NOT NULL ,
`id_foo` INT NULL DEFAULT NULL,
FOREIGN KEY ( `id_foo`) REFERENCES `Foo` (`id`) ON DELETE CASCADE ,
INDEX (`id_foo`),
INDEX (`x`),
UNIQUE (`id_foo`, `x`)
) ENGINE = INNODB DEFAULT CHARSET LATIN1;