MySQL 8.4Lab-verified · 2026-11-29

MySQL ERROR 1068 (42000): Multiple primary key defined

The statement would give the table a second primary key; a table can have only one.

MySQL 8.4.13
1068 (42000): Multiple primary key defined

Jump to a cause

  1. 01PRIMARY KEY written on two columns in CREATE TABLE
  2. 02ALTER TABLE MODIFY id ... AUTO_INCREMENT PRIMARY KEY when id is already the key
  3. 03ADD PRIMARY KEY on a table that already has one

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

A MySQL table has at most one primary key (it may span several columns). ERROR 1068 means the statement tries to define another one, either inside CREATE TABLE or with ALTER TABLE on a table that already has a primary key.

The three runs below are the usual ways this happens. SHOW KEYS FROM table WHERE Key_name = 'PRIMARY' shows the current primary key before changing anything.

01PRIMARY KEY written on two columns in CREATE TABLE

CREATE TABLE order_items (order_id INT PRIMARY KEY, line_no INT PRIMARY KEY, ...) failed with Multiple primary key defined. Each column-level PRIMARY KEY is a separate key.

Writing one composite key at the table level, PRIMARY KEY (order_id, line_no), worked. information_schema.key_column_usage showed PRIMARY on order_id (1) and line_no (2).

Reproducerun as lab_a
CREATE TABLE order_items (order_id INT PRIMARY KEY, line_no INT PRIMARY KEY, product VARCHAR(50));
✕ ERROR
ERROR 1068 (42000): Multiple primary key defined
Fixrun as lab_a
CREATE TABLE order_items (order_id INT, line_no INT, product VARCHAR(50), PRIMARY KEY (order_id, line_no));
OUTPUT
Query OK, 0 rows affected (0.01 sec)
After the fixrun as lab_a
SELECT constraint_name, column_name, ordinal_position FROM information_schema.key_column_usage WHERE table_schema = 'lab_a' AND table_name = 'order_items' ORDER BY ordinal_position;
✓ SUCCESS
+-----------------+-------------+------------------+
| CONSTRAINT_NAME | COLUMN_NAME | ORDINAL_POSITION |
+-----------------+-------------+------------------+
| PRIMARY         | order_id    |                1 |
| PRIMARY         | line_no     |                2 |
+-----------------+-------------+------------------+
2 rows in set (0.00 sec)

02ALTER TABLE MODIFY id ... AUTO_INCREMENT PRIMARY KEY when id is already the key

customers.id was already the primary key. Adding AUTO_INCREMENT with ALTER TABLE customers MODIFY id INT NOT NULL AUTO_INCREMENT PRIMARY KEY failed with 1068, because PRIMARY KEY in MODIFY asks for a new key.

SHOW KEYS showed PRIMARY on id. The same MODIFY without PRIMARY KEY succeeded; INSERT INTO customers (name) VALUES ('Park') then got id 3.

Setup
Setuprun as lab_a
CREATE TABLE customers (id INT NOT NULL PRIMARY KEY, name VARCHAR(50));
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
ALTER TABLE customers MODIFY id INT NOT NULL AUTO_INCREMENT PRIMARY KEY;
✕ ERROR
ERROR 1068 (42000): Multiple primary key defined
Checkrun as lab_a
SHOW KEYS FROM customers WHERE Key_name = 'PRIMARY';
OUTPUT
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table     | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| customers |          0 | PRIMARY  |            1 | id          | A         |           2 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.00 sec)
Fixrun as lab_a
ALTER TABLE customers MODIFY id INT NOT NULL AUTO_INCREMENT;
OUTPUT
Query OK, 2 rows affected (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 0
After the fixrun as lab_a
INSERT INTO customers (name) VALUES ('Park');
SELECT * FROM customers;
✓ SUCCESS
Query OK, 1 row affected (0.01 sec)

+----+------+
| id | name |
+----+------+
|  1 | Kim  |
|  2 | Lee  |
|  3 | Park |
+----+------+
3 rows in set (0.00 sec)

03ADD PRIMARY KEY on a table that already has one

order_items had a primary key on id. ALTER TABLE order_items ADD PRIMARY KEY (order_id, line_no) failed with 1068.

SHOW KEYS confirmed PRIMARY on id. Dropping and adding in one statement, ALTER TABLE order_items DROP PRIMARY KEY, ADD PRIMARY KEY (order_id, line_no), succeeded, and the primary key became order_id, line_no.

Setup
Setuprun as lab_a
CREATE TABLE order_items (id INT NOT NULL PRIMARY KEY, order_id INT NOT NULL, line_no INT NOT NULL);
INSERT INTO order_items VALUES (1, 10, 1), (2, 10, 2), (3, 11, 1);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
ALTER TABLE order_items ADD PRIMARY KEY (order_id, line_no);
✕ ERROR
ERROR 1068 (42000): Multiple primary key defined
Checkrun as lab_a
SHOW KEYS FROM order_items WHERE Key_name = 'PRIMARY';
OUTPUT
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table       | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| order_items |          0 | PRIMARY  |            1 | id          | A         |           3 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.00 sec)
Fixrun as lab_a
ALTER TABLE order_items DROP PRIMARY KEY, ADD PRIMARY KEY (order_id, line_no);
OUTPUT
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
After the fixrun as lab_a
SELECT column_name, seq_in_index FROM information_schema.statistics WHERE table_schema = 'lab_a' AND table_name = 'order_items' AND index_name = 'PRIMARY' ORDER BY seq_in_index;
✓ SUCCESS
+-------------+--------------+
| COLUMN_NAME | SEQ_IN_INDEX |
+-------------+--------------+
| order_id    |            1 |
| line_no     |            2 |
+-------------+--------------+
2 rows in set (0.00 sec)

FAQ

How do I make a primary key on two columns in MySQL?

Write it once at the table level: PRIMARY KEY (order_id, line_no). Do not put PRIMARY KEY after each column.

How do I add AUTO_INCREMENT to an existing primary key?

Use MODIFY without repeating PRIMARY KEY: ALTER TABLE t MODIFY id INT NOT NULL AUTO_INCREMENT;.

How do I change which columns form the primary key?

Drop and add in the same ALTER: ALTER TABLE t DROP PRIMARY KEY, ADD PRIMARY KEY (a, b);.

How do I see the current primary key?

SHOW KEYS FROM t WHERE Key_name = 'PRIMARY'; lists its columns in order.

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