MySQL 8.4Lab-verified · 2026-11-29

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

An index, primary key or unique key was put on a TEXT or BLOB column without a prefix length.

MySQL 8.4.13
1170 (42000): BLOB/TEXT column 'title' used in key specification without a key length

Jump to a cause

  1. 01Normal index on a TEXT column
  2. 02TEXT column as PRIMARY KEY
  3. 03UNIQUE key on a BLOB column (hash)

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4

MySQL cannot index a whole TEXT or BLOB value. When an index, PRIMARY KEY or UNIQUE key lists such a column without a length, CREATE or ALTER fails with ERROR 1170.

There are two ways out: index only the first N characters (title(100)), or change the column to VARCHAR / VARBINARY with a fixed maximum. Which one fits depends on whether the key has to be unique.

01Normal index on a TEXT column

posts.title is TEXT. CREATE INDEX idx_posts_title ON posts (title) failed with ERROR 1170 for 'title'. information_schema.columns showed data_type text.

CREATE INDEX idx_posts_title ON posts (title(100)) succeeded. SHOW INDEX showed Sub_part 100, meaning only the first 100 characters are indexed.

Setup
Setuprun as lab_a
CREATE TABLE posts (id INT PRIMARY KEY, title TEXT, body TEXT);
OUTPUT
Query OK, 0 rows affected (0.01 sec)
Reproducerun as lab_a
CREATE INDEX idx_posts_title ON posts (title);
✕ ERROR
ERROR 1170 (42000): BLOB/TEXT column 'title' used in key specification without a key length
Checkrun as lab_a
SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = 'lab_a' AND table_name = 'posts' ORDER BY ordinal_position;
OUTPUT
+-------------+-----------+
| COLUMN_NAME | DATA_TYPE |
+-------------+-----------+
| id          | int       |
| title       | text      |
| body        | text      |
+-------------+-----------+
3 rows in set (0.00 sec)
After the fixrun as lab_a
CREATE INDEX idx_posts_title ON posts (title(100));
SHOW INDEX FROM posts WHERE Key_name = 'idx_posts_title';
✓ SUCCESS
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name        | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| posts |          1 | idx_posts_title |            1 | title       | A         |           0 |      100 |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.01 sec)

02TEXT column as PRIMARY KEY

CREATE TABLE api_tokens (token TEXT PRIMARY KEY, ...) failed with ERROR 1170 for 'token'. A prefix would make the key unique only on the first N characters, so a fixed-size type fits better.

A SHA-256 hex string is 64 characters (CHAR_LENGTH(SHA2('token-1', 256)) = 64). The table was created with token VARCHAR(255) PRIMARY KEY.

Reproducerun as lab_a
CREATE TABLE api_tokens (token TEXT PRIMARY KEY, user_id INT);
✕ ERROR
ERROR 1170 (42000): BLOB/TEXT column 'token' used in key specification without a key length
Checkrun as lab_a
SELECT CHAR_LENGTH(SHA2('token-1', 256)) AS sha256_hex_len;
OUTPUT
+----------------+
| sha256_hex_len |
+----------------+
|             64 |
+----------------+
1 row in set (0.00 sec)
After the fixrun as lab_a
CREATE TABLE api_tokens (token VARCHAR(255) PRIMARY KEY, user_id INT);
✓ SUCCESS
Query OK, 0 rows affected (0.02 sec)

03UNIQUE key on a BLOB column (hash)

content_hash is BLOB. ALTER TABLE files ADD UNIQUE KEY uk_files_hash (content_hash) failed with ERROR 1170.

A binary SHA-256 is 32 bytes (LENGTH(UNHEX(SHA2('hello', 256))) = 32). After ALTER TABLE files MODIFY content_hash VARBINARY(32), the same UNIQUE key was added.

Setup
Setuprun as lab_a
CREATE TABLE files (id INT PRIMARY KEY, content_hash BLOB);
OUTPUT
Query OK, 0 rows affected (0.02 sec)
Reproducerun as lab_a
ALTER TABLE files ADD UNIQUE KEY uk_files_hash (content_hash);
✕ ERROR
ERROR 1170 (42000): BLOB/TEXT column 'content_hash' used in key specification without a key length
Checkrun as lab_a
SELECT LENGTH(UNHEX(SHA2('hello', 256))) AS sha256_bytes;
OUTPUT
+--------------+
| sha256_bytes |
+--------------+
|           32 |
+--------------+
1 row in set (0.00 sec)
After the fixrun as lab_a
ALTER TABLE files MODIFY content_hash VARBINARY(32);
ALTER TABLE files ADD UNIQUE KEY uk_files_hash (content_hash);
✓ SUCCESS
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

FAQ

How do I fix BLOB/TEXT column used in key specification without a key length?

Give a prefix length like title(100), or change the column to VARCHAR/VARBINARY. Both worked in the runs above.

Should I use a prefix index for a PRIMARY KEY or UNIQUE?

A prefix key checks uniqueness only on the first N characters. In the runs above, keys that must be unique used VARCHAR(255) and VARBINARY(32) instead.

How do I see the prefix length of an index?

SHOW INDEX FROM posts — the Sub_part column showed 100 for title(100).

Why is the Korean message in English?

MySQL 8.4's Korean message file has no translation for 1170, so lc_messages=ko_KR still printed BLOB column ... without a key length.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13