It struck me that error 1089 thrown presents three possible reasons:
1089 - Incorrect prefix key;
(a) the used key is not a string,
(b) the used length is longer than the key part,
(c) or the storage engine does not support unique prefiex keys
In your case the error is due to the first reason: the used key is not a string , and it happens because you are treating a column of type INT
as if it were of type VARCHAR
. The error would be corrected, as has been said in other answers, by removing the size of the column indicated in the declaration of the PRIMARY KEY
in this line:
Change:
PRIMARY KEY (clientId (10))
By:
PRIMARY KEY ('clientId')
Perooo.
It does not mean that indicating a length to PRIMARY KEY
is always incorrect .
In your documentation MySQL indicates that the key prefixes are valid in columns of the following types: CHAR, VARCHAR, BINARY, VARBINARY, BLOB y TEXT
.
Prefixes can be specified for CHAR, VARCHAR, BINARY, and VARBINARY
column indexes.
Prefixes must be specified for BLOB and TEXT column indexes.
In fact, in MYSQL, if the column of your table that will make the functions of PRIMARY KEY
is of type VARCHAR
you can perfectly indicate that you want only a prefix of that column as PRIMARY KEY
, that is, the n
characters indicated in parentheses from the left.
So a sentence like this is totally correct:
CREATE TABLE 'prensacarv1'.'pr_clients' (
'clientId' INT(10) NOT NULL,
'firstName' VARCHAR(45) NOT NULL,
'lastName' VARCHAR(45) NOT NULL,
-- otras columnas ...,
PRIMARY KEY ('firstName' (10))
) ENGINE=INNODB;
in it we indicate that the PRIMARY KEY
will be the first 10 characters of the name of the person.
On the other hand, if you do this:
CREATE TABLE 'prensacarv1'.'pr_clients' (
'clientId' INT(10) NOT NULL,
'firstName' VARCHAR(45) NOT NULL,
'lastName' VARCHAR(45) NOT NULL,
-- otras columnas ...,
PRIMARY KEY ('firstName' (999))
) ENGINE=INNODB;
You would be committing part (b) of the error message: the used length is longer than the key part , since you are telling it to take 999 characters when the length of the column is only 45.
For more information about the interesting error 1089 , see the next article .