Specified key was too long; max key length is 1000 bytes - mysql

I'm currently moving my concrete5.7 setup from localhost to a live server. First thing I did was export the SQL database and import it on the server, however this is giving me an error:
Error
SQL query:
-- --------------------------------------------------------
--
-- Tabelstructuur voor tabel `config`
--
CREATE TABLE IF NOT EXISTS `config` (
`configNamespace` VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`configGroup` VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL ,
`configItem` VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL ,
`configValue` LONGTEXT COLLATE utf8_unicode_ci,
PRIMARY KEY ( `configNamespace` , `configGroup` , `configItem` ) ,
KEY `configGroup` ( `configGroup` )
) ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
MySQL said:
#1071 - Specified key was too long; max key length is 1000 bytes
I'm aware that the key is too long but is there any freedom to change? I've tried changing the database collation to latin1 but it did not work.
What settings can I change?
Thank you for any feedback

That is a poor choice of primary key - you do not need 1000 bytes of entropy to make a row unique
If there is no natural choice of primary key, you can generate a surrogate key for the row.
e.g. replace
PRIMARY KEY ( `configNamespace` , `configGroup` , `configItem` ) ,
with
configId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
This should not be a breaking change for an application which uses the above.

I would recommend the following structure:
CREATE TABLE IF NOT EXISTS `config` (
configId int not null auto_increment primary key,
configNamespace VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
configGroup VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL ,
configItem VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL ,
configValue LONGTEXT COLLATE utf8_unicode_ci,
UNIQUE (configNamespace, configGroup, configItem) ,
KEY `configGroup` ( `configGroup` )
) ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
This creates the auto-incremented primary key for the table. In addition, it keeps the unique constraint on the three columns previously used for the primary key.

Related

I get the error: incorrect table definition [duplicate]

This question already has answers here:
There can be only one auto column
(6 answers)
Closed 2 years ago.
Im using phpmyadmin for the first time and I get this error: incorrect table definition. there can be only one auto column and it must be defined as key. What am I doing wrong?
This is my code:
CREATE TABLE `database_reservering`.`formData` (
`nameTeacher` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`nameChild` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `email` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`age` INT(11) NOT NULL ,
`date` DATE NOT NULL ,
`comment` VARCHAR(300) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`id` INT(30) UNSIGNED NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`nameTeacher`)
) ENGINE = InnoDB;
The actual error message is:
Incorrect table definition; there can be only one auto column and it must be defined as a key
The problem is that you have id as auto-increment, but the primary key is on nameTeacher. This is not allowed. You can change the statement to make id the primary key, and put a unique constraint on nameTeacher. This implements the same logic, but is valid MySQL syntax:
CREATE TABLE `formData` (
...
PRIMARY KEY (`id`),
UNIQUE (`nameTeacher`)
) ENGINE = InnoDB;
Demo on DB Fiddle
Change
PRIMARY KEY (nameTeacher)
to
PRIMARY KEY (id)
Full statement:
CREATE TABLE formData (
nameTeacher VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
nameChild VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
email VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
age INT(11) NOT NULL ,
`date` DATE NOT NULL ,
`comment` VARCHAR(300) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (id)) ENGINE = InnoDB;
What do you want to achieve when PK is set to the column other than AUTO_INCREMENT?
If you want to have separate independent autoincremented sequence for each nameTeacher value then alter the engine to MyISAM and define PK like (nameTeacher, id):
CREATE TABLE `formData` (
`nameTeacher` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`nameChild` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`email` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`age` INT(11) NOT NULL , `date` DATE NOT NULL ,
`comment` VARCHAR(300) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`id` INT(30) UNSIGNED NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`nameTeacher`, `id`)
) ENGINE = MyISAM;
fiddle
See Using AUTO_INCREMENT for details.

#1075 MySQL Error

So i am just a beginner in all this php stuff. I know just the basics, and when i setting up the settings for my new table, I met the problem #1075. Before, i created one, almost similar to this one, and i don't see the differenc. Can you say me where is the problem and explain what is happening?
CREATE TABLE `try`.`testing` ( `id` INT NOT NULL AUTO_INCREMENT , `date` DATE NOT NULL , `text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ) ENGINE = MyISAM;
here is the code of my SQL Preview. I use phpMyAdmin, obviously.
Please, help me.
Thank, you)
Try this
CREATE TABLE `testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MYISAM ;
You have to declare your AUTO_INCREMENT field as a primary key or a key. So you have to add PRIMARY KEY (id) or KEY (id) to your CREATE TABLE statement:
CREATE TABLE `try`.`testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL ,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) -- as primary key
KEY (`id`) -- or as key
) ENGINE = MyISAM;
Please also check:
https://stackoverflow.com/a/8114994/3647441
https://stackoverflow.com/a/14087703/3647441
For an autoincrement field you should have some sort of index associated with it. eg: primary key which is missing
Try This.
CREATE TABLE `try`.`testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL ,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
KEY (`id`)
) ENGINE = MyISAM;
https://dev.mysql.com/doc/refman/5.6/en/example-auto-increment.html

MySQL unique 1500 varchar field error (#1071 - Specified key was too long)

I have a field (link) that is varchar (1500) and that I want to make unique. I applied changes to mysql configuaration and increased length to 3072 bytes
ROW_FORMAT=DYNAMIC, innodb_file_format = Barracuda, innodb_large_prefix = true
But when I apply unique to my field, I got next error:
"#1071 - Specified key was too long; max key length is 3072 bytes"
My field is varchar(1500) that is 3000 bytes.
What's wrong?
Update (1)
Table data:
CREATE TABLE IF NOT EXISTS `pages` (
`link` varchar(1500) NOT NULL,
`domain` varchar(255) NOT NULL,
`lastvisited` datetime DEFAULT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
KEY `link` (`link`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ROW_FORMAT=DYNAMIC;
Update (2)
Alter command (done via PHPMYADMIN)
ALTER TABLE `pages` ADD UNIQUE (
`link`
)
Since you will be storing URLs in the link column, you don't actually need to use UTF8 for it, because URLs can contain only ASCII characters. Specifying a plain ASCII character encoding for your link column will even allow you to raise its max length to 3072 characters.
CREATE TABLE IF NOT EXISTS `pages` (
`link` varchar(1500) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
`domain` varchar(255) NOT NULL,
`lastvisited` datetime DEFAULT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE KEY `link` (`link`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ROW_FORMAT=DYNAMIC;
(Updated as per #eggyal's suggestion for the ascii_bin collation)

why this error occur #1071 - Specified key was too long; max key length is 1000 bytes? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
“Specified key was too long; max key length is 1000 bytes”
SQL query:
CREATE TABLE `freecomputermarket`.`Members` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`UserName` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`Email` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`Password` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`BirthDate` DATE NOT NULL ,
`RegisterationDate` DATE NOT NULL ,
`ActivationCode` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`ActivationLink` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`IsActive` BIT( 0 ) NOT NULL ,
`Gender` CHAR( 6 ) NOT NULL ,
UNIQUE (
`UserName` ,
`ActivationCode` ,
`ActivationLink`
)
) ENGINE = MYISAM
when i am executing this query an error "#1071 - Specified key was too long; max key length is 1000 bytes" raised?
You have three 255-character UTF8 columns in your UNIQUE index. Each UTF8 character can take up to 3 bytes, so each column can take up to 765 bytes, which makes a total of 2295 bytes for your entire index, which as the error states, is over the limit of 1000 bytes.

Mysql errno: 150 Cannot create Table

i use mysql work bench to export sql from erd that i create, but when i try to importing it to mysql, i just get error 150 Cannot create table.
when i try to remove constrain of service child, its just work. but i need to keep those referenced.
and every table that reference to service table, also have this error too. where am i wrong ?
CREATE TABLE IF NOT EXISTS `service` (
`id` INT(11) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(200) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL ,
`seq` INT(11) NOT NULL ,
`image` VARCHAR(200) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `service_package` (
`id` INT(11) NOT NULL AUTO_INCREMENT ,
`sc_id` INT(11) NOT NULL ,
`lang` INT(11) NOT NULL ,
`package` VARCHAR(200) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `service_package_id` (`sc_id` ASC) ,
CONSTRAINT `service_package_id`
FOREIGN KEY (`sc_id` )
REFERENCES `service` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
You're using ENGINE = MyISAM on the service table. Try using ENGINE = InnoDB instead (as you do on the service_package table).
Foreign keys do not work with the MyIsam storage engine.
For others viewing this thread, here are some other reasons why this happens:
1) The parent column has to be indexed before you create the foreign key.
2) The parent column needs to exist (seems simple, but it doesn't tell you that's the problem, it just says Errno 150.
There are a number of other reasons too. See the link below for an exhaustive list:
MySQL Foreign Key Errors and Errno: 150