MySQL
Database Management
Primary Key
SQL Tutorial
SQL Commands

Remove Primary Key in MySQL

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Removing a primary key in MySQL is mechanically simple, but the schema consequences are not. A primary key is often tied to indexing, foreign-key design, AUTO_INCREMENT, and application assumptions, so the right workflow is to inspect the table first and drop the key only after you know what else depends on it.

The Command

The SQL syntax is straightforward:

sql
ALTER TABLE orders DROP PRIMARY KEY;

That removes the primary-key constraint from the table. If the primary key is backed by a unique index created for that role, MySQL also removes that index as part of dropping the primary key.

Inspect The Table Before Changing It

Always look at the table definition first:

sql
SHOW CREATE TABLE orders;

Example starting table:

sql
1CREATE TABLE orders (
2    id BIGINT NOT NULL AUTO_INCREMENT,
3    customer_id BIGINT NOT NULL,
4    created_at DATETIME NOT NULL,
5    PRIMARY KEY (id)
6);

If you run ALTER TABLE orders DROP PRIMARY KEY;, the table loses its primary key, and any assumptions built around id being the canonical row identifier are now your problem to replace.

What Usually Breaks Or Changes

A primary key is not just a uniqueness rule. It often acts as:

  • the clustered identifier used for efficient lookup in InnoDB
  • the referenced column for foreign keys in other tables
  • the target of ORM assumptions about identity
  • the required indexed column for AUTO_INCREMENT

That last point matters a lot. In MySQL, an AUTO_INCREMENT column must be indexed. If you drop the primary key from a table where the auto-increment column has no other compatible index, the operation can fail or require schema adjustments.

A safer sequence is often to replace the primary key rather than leave the table without one.

Replacing Instead Of Just Dropping

Suppose you want to move from a single-column key to a composite key. Do it in one controlled migration.

sql
ALTER TABLE orders
    DROP PRIMARY KEY,
    ADD PRIMARY KEY (customer_id, created_at, id);

Or if you want to keep id indexed for AUTO_INCREMENT but change the primary key role, you might first add a separate index:

sql
ALTER TABLE orders ADD INDEX idx_orders_id (id);
ALTER TABLE orders DROP PRIMARY KEY;

Then add the new primary key or alternative uniqueness constraints immediately.

Example End-To-End

Create a sample table:

sql
1CREATE TABLE demo_users (
2    id INT NOT NULL AUTO_INCREMENT,
3    email VARCHAR(255) NOT NULL,
4    PRIMARY KEY (id)
5);

Inspect it:

sql
SHOW CREATE TABLE demo_users;

Drop the primary key:

sql
ALTER TABLE demo_users DROP PRIMARY KEY;

Inspect again:

sql
SHOW CREATE TABLE demo_users;

This sequence is runnable, but it should only be done when you intentionally want a table without that primary-key constraint.

When Removing The Primary Key Is Reasonable

There are valid cases:

  • a legacy table is being redesigned
  • a single-column key is being replaced by a composite key
  • a staging table temporarily does not need relational constraints
  • a mistaken schema migration created the wrong primary key

What is usually not reasonable is dropping a primary key just to work around duplicate data or import failures. In those cases, the data model probably needs cleanup, not weaker identity rules.

Foreign Keys And Application Code

Before dropping the key, search for dependencies. Other tables may reference the primary-key column with foreign keys, and your application code may assume records can always be located by that identifier.

If you remove the primary key without replacing the access pattern, queries may still run, but correctness and performance can quietly degrade.

Common Pitfalls

The biggest mistake is dropping the primary key from a production table without checking foreign keys, ORM mappings, and query plans first.

Another common mistake is forgetting the interaction with AUTO_INCREMENT. If the auto-increment column no longer has a suitable index, the migration may fail or produce a schema you did not intend.

Developers also treat primary keys and unique constraints as interchangeable. They are related, but they are not the same thing operationally.

Finally, do not leave important transactional tables without a clear stable row identifier unless you have a strong reason and understand the cost.

Summary

  • The syntax is ALTER TABLE ... DROP PRIMARY KEY.
  • Inspect the table with SHOW CREATE TABLE before changing it.
  • Check dependencies such as foreign keys, AUTO_INCREMENT, and ORM expectations.
  • In many cases, replacing the primary key is safer than removing it outright.
  • Dropping the key is easy; preserving data integrity afterward is the real task.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.