I have a table with a primary key that is a varchar(255). Some cases have arisen where 255 characters isn\'t enough. I tried changing the field to a text, but I get the foll
Don't have long values as primary key. That will destroy your performance. See the mysql manual, section 13.6.13 'InnoDB Performance Tuning and Troubleshooting'.
Instead, have a surrogate int key as primary (with auto_increment), and your loong key as a secondary UNIQUE.
alter table authors ADD UNIQUE(name_first(767), name_second(767));
NOTE : 767 is the number of characters limit upto which MySQL will index columns while dealing with blob/text indexes
Ref : http://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html
The solution to the problem is that in your CREATE TABLE
statement, you may add the constraint UNIQUE ( problemtextfield(300) )
after the column create definitions to specify a key
length of 300
characters for a TEXT
field, for example. Then the first 300
characters of the problemtextfield
TEXT
field would need to be unique, and any differences after that would be disregarded.
Another excellent way of dealing with this is to create your TEXT field without the unique constraint and add a sibling VARCHAR field that is unique and contains a digest (MD5, SHA1, etc.) of the TEXT field. Calculate and store the digest over the entire TEXT field when you insert or update the TEXT field then you have a uniqueness constraint over the entire TEXT field (rather than some leading portion) that can be searched quickly.
The error happens because MySQL can index only the first N chars of a BLOB or TEXT
column. So The error mainly happens when there is a field/column type of TEXT
or BLOB or those belong to TEXT
or BLOB
types such as TINYBLOB
, MEDIUMBLOB
, LONGBLOB
, TINYTEXT
, MEDIUMTEXT
, and LONGTEXT
that you try to make a primary key or index. With full BLOB
or TEXT
without the length value, MySQL is unable to guarantee the uniqueness of the column as it’s of variable and dynamic size. So, when using BLOB
or TEXT
types as an index, the value of N must be supplied so that MySQL can determine the key length. However, MySQL doesn’t support a key length limit on TEXT
or BLOB
. TEXT(88)
simply won’t work.
The error will also pop up when you try to convert a table column from non-TEXT
and non-BLOB
type such as VARCHAR
and ENUM
into TEXT
or BLOB
type, with the column already been defined as unique constraints or index. The Alter Table SQL command will fail.
The solution to the problem is to remove the TEXT
or BLOB
column from the index or unique constraint or set another field as primary key. If you can't do that, and wanting to place a limit on the TEXT
or BLOB
column, try to use VARCHAR
type and place a limit of length on it. By default, VARCHAR
is limited to a maximum of 255 characters and its limit must be specified implicitly within a bracket right after its declaration, i.e VARCHAR(200)
will limit it to 200 characters long only.
Sometimes, even though you don’t use TEXT
or BLOB
related type in your table, the Error 1170 may also appear. It happens in a situation such as when you specify VARCHAR
column as primary key, but wrongly set its length or characters size. VARCHAR
can only accepts up to 256 characters, so anything such as VARCHAR(512)
will force MySQL to auto-convert the VARCHAR(512)
to a SMALLTEXT
datatype, which subsequently fails with error 1170 on key length if the column is used as primary key or unique or non-unique index. To solve this problem, specify a figure less than 256 as the size for VARCHAR
field.
Reference: MySQL Error 1170 (42000): BLOB/TEXT Column Used in Key Specification Without a Key Length
You should define which leading portion of a TEXT
column you want to index.
InnoDB
has a limitation of 768
bytes per index key and you won't be able to create an index longer than that.
This will work fine:
CREATE TABLE t_length (
mydata TEXT NOT NULL,
KEY ix_length_mydata (mydata(255)))
ENGINE=InnoDB;
Note that the maximum value of the key size depends on the column charset. It's 767
characters for a single-byte charset like LATIN1
and only 255
characters for UTF8
(MySQL
only uses BMP
which requires at most 3
bytes per character)
If you need your whole column to be the PRIMARY KEY
, calculate SHA1
or MD5
hash and use it as a PRIMARY KEY
.