Common SQL Errors

MySQL Error 1068: Multiple primary key defined – How to Fix and Prevent

Galaxy Team
August 5, 2025

MySQL error 1068 appears when a table definition includes more than one PRIMARY KEY clause or index.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 1068?

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.

Error Highlights

Typical Error Message

Multiple primary key defined

Error Type

Constraint Error

Language

MySQL

Symbol

ER_MULTIPLE_PRI_KEY

Error Code

1068

SQL State

Explanation

Table of Contents

What is MySQL Error 1068: ER_MULTIPLE_PRI_KEY?

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.

What Causes This Error?

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.

How to Fix MySQL Error 1068: ER_MULTIPLE_PRI_KEY

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.

Common Scenarios and Solutions

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.

Best Practices to Avoid This Error

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.

Related Errors and Solutions

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.

.

Common Causes

Related Errors

FAQs

Can a MySQL table have more than one primary key?

No. MySQL enforces a single primary key per table. Use UNIQUE indexes for additional uniqueness requirements.

Does AUTO_INCREMENT require a primary key?

Yes. An AUTO_INCREMENT column must belong to the table’s single primary key or a key marked as primary.

How do I find current primary keys in MySQL?

Run SHOW CREATE TABLE table_name or query INFORMATION_SCHEMA.KEY_COLUMN_USAGE filtering by CONSTRAINT_NAME = 'PRIMARY'.

How does Galaxy help prevent this error?

Galaxy’s editor highlights duplicate primary key clauses in real time and its AI copilot suggests corrections before execution.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo