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
CREATE TABLE posts (id INT PRIMARY KEY, title TEXT, body TEXT);Query OK, 0 rows affected (0.01 sec)CREATE INDEX idx_posts_title ON posts (title);ERROR 1170 (42000): BLOB/TEXT column 'title' used in key specification without a key lengthSELECT column_name, data_type FROM information_schema.columns WHERE table_schema = 'lab_a' AND table_name = 'posts' ORDER BY ordinal_position;+-------------+-----------+
| COLUMN_NAME | DATA_TYPE |
+-------------+-----------+
| id | int |
| title | text |
| body | text |
+-------------+-----------+
3 rows in set (0.00 sec)CREATE INDEX idx_posts_title ON posts (title(100));
SHOW INDEX FROM posts WHERE Key_name = 'idx_posts_title';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.
CREATE TABLE api_tokens (token TEXT PRIMARY KEY, user_id INT);ERROR 1170 (42000): BLOB/TEXT column 'token' used in key specification without a key lengthSELECT CHAR_LENGTH(SHA2('token-1', 256)) AS sha256_hex_len;+----------------+
| sha256_hex_len |
+----------------+
| 64 |
+----------------+
1 row in set (0.00 sec)CREATE TABLE api_tokens (token VARCHAR(255) PRIMARY KEY, user_id INT);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
CREATE TABLE files (id INT PRIMARY KEY, content_hash BLOB);Query OK, 0 rows affected (0.02 sec)ALTER TABLE files ADD UNIQUE KEY uk_files_hash (content_hash);ERROR 1170 (42000): BLOB/TEXT column 'content_hash' used in key specification without a key lengthSELECT LENGTH(UNHEX(SHA2('hello', 256))) AS sha256_bytes;+--------------+
| sha256_bytes |
+--------------+
| 32 |
+--------------+
1 row in set (0.00 sec)ALTER TABLE files MODIFY content_hash VARBINARY(32);
ALTER TABLE files ADD UNIQUE KEY uk_files_hash (content_hash);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: 0FAQ
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