How to Fix MySQL Error 1068 (ER_MULTIPLE_PRI_KEY)

Published
Length
3 min read

Luis Felipe Dias

All posts

You trigger mysql error 1068 when a statement tries to add a second primary key to a table. To fix it, drop the existing primary key first or use a composite key or unique index instead. In MySQL, every table can hold only one primary key constraint. When you run a schema change that breaks this rule, the database engine stops the action right away. It returns symbol ER_MULTIPLE_PRI_KEY with SQLSTATE 42000 and the message that multiple primary keys are defined. Understanding why your migration

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You trigger mysql error 1068 when a statement tries to add a second primary key to a table. To fix it, drop the existing primary key first or use a composite key or unique index instead.

In MySQL, every table can hold only one primary key constraint. When you run a schema change that breaks this rule, the database engine stops the action right away. It returns symbol ER_MULTIPLE_PRI_KEY with SQLSTATE 42000 and the message that multiple primary keys are defined. Understanding why your migration or script asks for a second key helps you fix the schema without losing data.

What causes mysql error 1068 in database schemas

The database engine uses the primary key as the main physical index for table rows. Because the storage engine orders data around this single key, a table cannot have two of them. You will see mysql error 1068 during two common actions. First, a developer might write a table creation script that marks two separate columns as the primary key. Second, an automated migration tool might try to add a primary key to an old table that already has one.

Sometimes teams want two columns to identify a row together. Marking both columns as separate primary keys triggers the 1068 failure code. Instead, the schema needs a single composite primary key that groups both columns under one rule. In other cases, an engineer just wants to make sure a new column holds unique values, like emails or phone numbers. Adding a unique index achieves that goal without touching the primary key at all.

How to inspect the table before making changes

Before you run a fix in a live system, check the current table structure first. Read the table metadata to see which column acts as the current primary key. You also need to check if the current key uses an auto-increment rule. MySQL requires auto-increment columns to stay indexed at all times.

Next, check if any child tables link to this primary key through foreign key rules. If you drop a primary key that other tables depend on, those links will break or block your change. Reviewing the table layout takes only a minute and prevents downtime during your release.

API Key Management + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

How to fix the schema safely in production

Once you know the current table layout, pick the right fix for your data model. If the second column only needs unique values and no null entries, change your script to create a unique constraint instead. This approach leaves the existing primary key intact and avoids heavy table rebuilds.

If you need to replace the old primary key or build a composite key, combine the steps into one alter action. Drop the old primary key and add the new primary key in a single statement. Make sure any auto-increment column keeps a standard index before you drop its primary key status. Always run these schema changes through a reviewed workflow rather than sharing broad admin credentials with the whole team. Limit schema change permissions to approved sessions so nobody drops a core index by mistake.

Check MySQL queries and results on the wire

MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.

Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.

Frequently asked questions

Can a MySQL table have two primary keys?

No, a MySQL table can have only one primary key. Still, that single primary key can include multiple columns, which is called a composite primary key.

How do I enforce unique values on a second column without triggering error 1068?

Add a unique index and a not-null constraint to the second column. This setup blocks duplicate and empty values just like a primary key does.

Why does dropping a primary key fail on an auto-increment column?

MySQL requires every auto-increment column to be part of an index. You must remove the auto-increment rule or add a regular index before dropping the primary key.

The query runs. The emails stay hidden.