Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-30 · container image mysql:8.4
Index names must be unique within a table. CREATE INDEX or ALTER TABLE ... ADD INDEX with a name that is already used fails with ERROR 1061, even if the columns are different. The message shows the name, e.g. 'idx_orders_customer'. MySQL 8.4 has no CREATE INDEX IF NOT EXISTS, so a rerun-safe script has to check first.
Below are two cases reproduced on MySQL 8.4 and the fixes that were run.
01The same CREATE INDEX run again (migration or script rerun)
idx_orders_customer was already created, and running CREATE INDEX idx_orders_customer ON orders (customer_id) again failed with ERROR 1061. information_schema.STATISTICS showed the index on customer_id.
The script was changed to count the index in information_schema.STATISTICS first and build the DDL with IF(), then run it with PREPARE/EXECUTE. Because the index existed, it ran the alternative SELECT and printed index already exists without error.
Setup
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, created_at DATETIME);
CREATE INDEX idx_orders_customer ON orders (customer_id);Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0CREATE INDEX idx_orders_customer ON orders (customer_id);ERROR 1061 (42000): Duplicate key name 'idx_orders_customer'SELECT INDEX_NAME, COLUMN_NAME FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'lab_a' AND TABLE_NAME = 'orders' ORDER BY INDEX_NAME, SEQ_IN_INDEX;+---------------------+-------------+
| INDEX_NAME | COLUMN_NAME |
+---------------------+-------------+
| idx_orders_customer | customer_id |
| PRIMARY | id |
+---------------------+-------------+
2 rows in set (0.01 sec)If the existing index is on the same columns, nothing needs to change; just skip the CREATE.
SET @exists = (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'orders' AND INDEX_NAME = 'idx_orders_customer');
SET @ddl = IF(@exists = 0, 'CREATE INDEX idx_orders_customer ON orders (customer_id)', 'SELECT ''index already exists'' AS note');
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Statement prepared
+----------------------+
| note |
+----------------------+
| index already exists |
+----------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)02A new index on another column reusing an existing name
idx_orders already existed on customer_id, and ALTER TABLE orders ADD INDEX idx_orders (created_at) failed with Duplicate key name 'idx_orders'. The columns are different, but the name is the same.
With a distinct name, idx_orders_created_at, the index was added, and the table had idx_orders, idx_orders_created_at and PRIMARY.
Setup
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, created_at DATETIME);
CREATE INDEX idx_orders ON orders (customer_id);Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0ALTER TABLE orders ADD INDEX idx_orders (created_at);ERROR 1061 (42000): Duplicate key name 'idx_orders'SELECT INDEX_NAME, COLUMN_NAME FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'lab_a' AND TABLE_NAME = 'orders' ORDER BY INDEX_NAME;+------------+-------------+
| INDEX_NAME | COLUMN_NAME |
+------------+-------------+
| idx_orders | customer_id |
| PRIMARY | id |
+------------+-------------+
2 rows in set (0.00 sec)ALTER TABLE orders ADD INDEX idx_orders_created_at (created_at);
SELECT INDEX_NAME, COLUMN_NAME FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'lab_a' AND TABLE_NAME = 'orders' ORDER BY INDEX_NAME;Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-----------------------+-------------+
| INDEX_NAME | COLUMN_NAME |
+-----------------------+-------------+
| idx_orders | customer_id |
| idx_orders_created_at | created_at |
| PRIMARY | id |
+-----------------------+-------------+
3 rows in set (0.00 sec)FAQ
Does MySQL support CREATE INDEX IF NOT EXISTS?
Not in MySQL 8.4. Check information_schema.STATISTICS first and run the CREATE INDEX only when the count is 0, as in the first case.
How do I see existing index names?
SHOW INDEX FROM table_name, or SELECT INDEX_NAME, COLUMN_NAME FROM information_schema.STATISTICS for the table.
Can two indexes have the same name if they are on different columns?
No. Names must be unique per table regardless of columns; the second case failed with different columns.
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