MySQL error 1068 appears when a table definition includes more than one PRIMARY KEY clause or index.
MySQL Error 1068: ER_MULTIPLE_PRI_KEY occurs when a CREATE TABLE or ALTER TABLE statement tries to define two PRIMARY KEY constraints on the same table. Drop the extra key or convert it to a UNIQUE index to resolve the issue.
Multiple primary key defined
MySQL throws error 1068 with the message "Multiple primary key defined" when it detects more than one PRIMARY KEY specification in a single table definition. MySQL allows only one primary key per table because that key enforces a unique, non-null identifier for every row.
This error can surface during both CREATE TABLE and ALTER TABLE operations.
It often appears after schema migrations, copy-pasted DDL, or GUI-driven changes that inadvertently duplicate primary key declarations.
Defining a second PRIMARY KEY constraint in the CREATE TABLE statement immediately triggers the error. MySQL reads the DDL sequentially and halts at the duplicate declaration.
Running ALTER TABLE ... ADD PRIMARY KEY on a column set that already owns a primary key also raises error 1068.
Migration tools that apply incremental DDL changes are frequent culprits.
Identify which key should remain the table’s sole primary key. Then modify the table definition by dropping the redundant primary key or converting it into a UNIQUE index.
Validate your fix by re-running the ALTER TABLE or CREATE TABLE command.
The statement should complete without errors once only one primary key exists.
During imports from other databases, auto-generated DDL may explicitly add an id PRIMARY KEY and later mark another column set as PRIMARY KEY. Delete or alter one clause before executing.
Framework migrations may attempt to recreate a primary key during rollbacks.
Add logic to drop existing primary keys first or wrap statements in IF NOT EXISTS checks.
Enforce code reviews on DDL scripts and flag multiple PRIMARY KEY clauses in CI pipelines. Convention: always define the primary key at the top of the CREATE TABLE statement.
Use Galaxy’s schema-aware AI copilot to detect conflicting primary-key declarations before executing queries.
The editor highlights constraint duplication in real time.
Error 1215 "Cannot add foreign key constraint" may follow if a duplicate primary key prevents creation of referenced keys.
Error 1075 "Incorrect table definition; there can be only one auto column" often appears alongside 1068 because an AUTO_INCREMENT column must also belong to the single primary key.
.
No. MySQL enforces a single primary key per table. Use UNIQUE indexes for additional uniqueness requirements.
Yes. An AUTO_INCREMENT column must belong to the table’s single primary key or a key marked as primary.
Run SHOW CREATE TABLE table_name or query INFORMATION_SCHEMA.KEY_COLUMN_USAGE filtering by CONSTRAINT_NAME = 'PRIMARY'.
Galaxy’s editor highlights duplicate primary key clauses in real time and its AI copilot suggests corrections before execution.