MySQL
database management
primary key update
SQL tutorials
data integrity

Updating MySQL primary key

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Updating a MySQL primary key can mean two different operations: changing the value stored in a primary-key column, or changing which column or columns form the primary key definition. The first is done with UPDATE, while the second is a schema change that usually uses ALTER TABLE and requires more care because foreign keys, indexes, and application assumptions may all be affected.

Understand Which Change You Actually Need

Developers often say "update the primary key" when they really mean one of these:

  • change a row's existing primary-key value
  • replace the primary key with another column or composite key
  • convert an integer key to AUTO_INCREMENT

Those are not the same operation. The SQL, the risk, and the migration plan differ substantially.

Changing the Value of a Primary-Key Column

If the primary key is not generated automatically and you really need to change a specific row's key value, you can use UPDATE just as you would for another column.

sql
UPDATE users
SET user_id = 2001
WHERE user_id = 1001;

That only succeeds safely if the new value is still unique and does not violate any foreign-key relationship.

If related tables reference that key, you need to know whether the foreign keys are defined with ON UPDATE CASCADE. If they are not, the update may fail or leave application logic broken.

A parent-child example:

sql
1CREATE TABLE customers (
2    customer_id INT PRIMARY KEY,
3    name VARCHAR(100)
4);
5
6CREATE TABLE orders (
7    order_id INT PRIMARY KEY,
8    customer_id INT,
9    CONSTRAINT fk_orders_customer
10        FOREIGN KEY (customer_id)
11        REFERENCES customers(customer_id)
12        ON UPDATE CASCADE
13);

With ON UPDATE CASCADE, changing customers.customer_id can propagate to orders.customer_id automatically.

Changing Which Column Is the Primary Key

If you want a different column to become the primary key, that is a schema migration.

sql
ALTER TABLE users
DROP PRIMARY KEY,
ADD PRIMARY KEY (email);

This is much riskier than changing one row value because MySQL enforces uniqueness and non-null requirements on primary keys. Before running a migration like this, confirm that the target column:

  • contains no duplicates
  • contains no NULL values
  • is appropriate for long-term identity semantics

Natural keys such as email addresses often look convenient at first and become painful later when business rules change.

Working Safely With Foreign Keys

Primary keys are often referenced all over the schema. Before changing either the key value or key definition, inspect dependent constraints.

Useful diagnostic query:

sql
1SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME
2FROM information_schema.KEY_COLUMN_USAGE
3WHERE REFERENCED_TABLE_SCHEMA = DATABASE()
4  AND REFERENCED_TABLE_NAME = 'users';

That tells you which child tables depend on the target table. It is a good habit to inspect those dependencies before any key migration, not after something fails in production.

Prefer Surrogate Keys for Stability

A frequent design lesson here is that keys that might change are poor primary keys. If your identifier can be edited by users or business rules, consider storing it as a unique secondary column and using a stable surrogate key as the primary key.

For example:

sql
1CREATE TABLE users (
2    user_id BIGINT PRIMARY KEY AUTO_INCREMENT,
3    email VARCHAR(255) NOT NULL UNIQUE
4);

Then email can change when needed without forcing a primary-key update across related tables.

Use Transactions and Migration Planning

For small data corrections, a transaction can help you validate and roll back if needed.

sql
1START TRANSACTION;
2
3UPDATE users
4SET user_id = 2001
5WHERE user_id = 1001;
6
7COMMIT;

For schema-level primary-key changes, use a migration plan, not an ad hoc console edit. Application deployments, ORM mappings, and downstream queries may all need coordinated updates.

Common Pitfalls

The biggest mistake is changing a primary key without checking dependent foreign keys. Even when the database blocks the change safely, the failure can happen mid-deployment and leave you scrambling.

Another mistake is using a mutable business field as the primary key when the field is likely to change. That design creates avoidable migration pain later.

People also forget that changing the primary key definition changes the table's main unique index. On large tables, that can be expensive and may require maintenance planning.

Finally, do not assume UPDATE and ALTER TABLE solve the same problem. One changes data values; the other changes schema structure.

Summary

  • Updating a primary key value and redefining the primary key are different operations.
  • Use UPDATE to change a row's key value, but only after checking uniqueness and foreign-key effects.
  • Use ALTER TABLE to change which column or columns form the primary key.
  • Inspect foreign-key dependencies before touching a primary key.
  • Stable surrogate keys usually make future schema changes much easier.

Course illustration
Course illustration

All Rights Reserved.