Incorrect table definition — only one auto column and it must be a key
Production Risk
Low — DDL fails; no data loss.
ER_WRONG_AUTO_KEY (1121, SQLSTATE 42000) is raised when an AUTO_INCREMENT column is defined but is not the first column of an index or PRIMARY KEY. This is a variant of 1075 with a slightly different trigger condition.
- 1AUTO_INCREMENT column is part of a composite key but is not the leftmost column
- 2AUTO_INCREMENT column has no index at all
CREATE TABLE t (id INT AUTO_INCREMENT, cat INT, PRIMARY KEY (cat, id));
expected output
ERROR 1121 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
Fix
Make the AUTO_INCREMENT column the first column of its key
WHEN AUTO_INCREMENT is in a composite key.
CREATE TABLE t ( id INT AUTO_INCREMENT, cat INT, PRIMARY KEY (id), INDEX idx_cat (cat) );
Why this works
InnoDB requires AUTO_INCREMENT to be the leftmost column in at least one index so it can find the max value efficiently.
✕ Use a composite primary key with AUTO_INCREMENT as a non-leading column
MySQL does not allow this; redesign with AUTO_INCREMENT as the primary key and add the category as a separate indexed column.
MySQL 8.0 — 1121 ER_WRONG_AUTO_KEY
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev