MySQL Error 1170 (42000): BLOB/TEXT Column Used in Key Specification Without a Key Length

When creating a new table or altering an existing table with primary keys, unique constraints and indexes, or when defining a new index with Alter Table manipulation statement in MySQL database, the following error may occur and prohibit the the command from completing:

ERROR 1170 (42000): BLOB/TEXT column ‘field_name’ used in key specification without a key length

The error happens because MySQL can index only the first N chars of a BLOB or TEXT column. So The error mainly happen when there is a field/column type of TEXT or BLOB or those belongs to TEXT or BLOB types such as TINYBLOB, MEDIUMBLOB, LONGBLOB, TINYTEXT, MEDIUMTEXT, and LONGTEXT that you try to make as 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 index, the value of N must be supplied so that MySQL can determine the key length. However, MySQL doesn’t support 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 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 fail 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.


9 Responses to “MySQL Error 1170 (42000): BLOB/TEXT Column Used in Key Specification Without a Key Length”

  1. Eli Manobella
    April 30th, 2009 22:32
    9

    Thank you for the post.
    I need a key that is longer then 256.

    What can I do?

    googling it for the past 3 hours with no luck…
    (Paco Zarabozo comment did not work for me)

  2. Preaching to the UTF8 converted at hontou ni sou omou?
    December 13th, 2008 22:20
    8

    [...] I ran into some problems, I back-pedalled to square one. Thankfully my backup restored the Latin1 database without any [...]

  3. Paco Zarabozo
    October 29th, 2008 10:16
    7

    Well, here’s another solution. You can actually specify the key length for your index while keeping your field as longtext. This is an example:

    create table messages (
    id int(20) not null auto_increment,
    message longtext,
    index (message(996), id)
    );

    desc messages;
    show index from messages;

    996 is the maximium key length mySQL has accepted for me.

    Cheers,

    Paco

  4. David
    October 27th, 2008 19:50
    6

    Thanks a lot!

  5. Marius Vilaia
    October 6th, 2008 03:50
    5

    Thank you very much. I had this problem and you were a life saver!

  6. Reza
    March 7th, 2008 04:15
    4

    Thanks, very good defined

  7. Jamie Slater
    February 23rd, 2008 03:40
    3

    Thanks for the post. Very helpful.

  8. Ben Hardy
    February 20th, 2008 02:45
    2

    Thanks! Very useful post.

  9. Felix
    August 30th, 2007 18:33
    1

    Thanks for putting this up. I didn’t get the error and you saved me a lot o frustration. I’d put you in the ‘Goog people’ category anytime ;-)

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to comments feature has been disabled. To receive notification of latest comments posted, subscribe to My Digital Life Comments RSS feed or register to receive new comments in daily email digest.
Custom Search

New Articles

Incoming Search Terms for the Article

used in key specification without a key length - used in key specification without a key length - BLOB/TEXT column used in key specification without a key length - mysql error 1170 - blob text column used in key specification without a key length - BLOB/TEXT column used in key specification without a key length - MYSQL #1170 - "used in key specification without a key length" - mysql used in key specification without a key length - MySQL 1170 - ERROR 1170 (42000) - #1170 - BLOB/TEXT column used in key specification without a key length - #1170 - BLOB/TEXT column 'Name' used in key specification without a key length - mysql BLOB/TEXT column used in key specification without a key length - BLOB/TEXT column 'name' used in key specification without a key length - column used in key specification without a key length - used in key specification without a key length mysql - ERROR 1170 (42000): BLOB/TEXT column used in key specification without a key length - used in key specification without a key length - BLOB/TEXT column 'value' used in key specification without a key length - The used table type doesn't support BLOB/TEXT columns - 1170 blob text column used in key specification without a key length - mysql text key - ERROR 1170 mysql - mysql key length - #1170 - BLOB/TEXT column - used in key specification without a key length - BLOB/TEXT column 'URL' used in key specification without a key length - key specification without a key length - #1170 - BLOB/TEXT column used in key specification without a key length - BLOB/TEXT column used in key specification without a key length - #1170 - BLOB/TEXT column 'content' used in key specification without a key length - BLOB/TEXT column 'real_name' used in key specification without a key length - ERROR 1170 (42000): BLOB/TEXT column - ERROR 1170 (42000): BLOB/TEXT column 'name' used in key specification without a key length - #1170 - BLOB/TEXT column '' used in key specification without a key length - #1170 - BLOB/TEXT column used in key specification without a key length - can't be used in key specification with the used table type - #1170 - BLOB/TEXT column 'url' used in key specification without a key length - MYSQL error #1170 - used in key specification without key length - #1170 - BLOB/TEXT column used in key specification without a key length - mysql error code 1170 - BLOB/TEXT column 'URL' used in key specification without a key length - mysql error used in key specification without a key length - mysql blob key length - BLOB/TEXT column 'object_pk' used in key specification without a key length - BLOB/TEXT column 'Summary' used in key specification without a key length - mysql::error: blob/text - BLOB/TEXT column 'label' used in key specification without a key length -