Difference Between PRIMARY KEY and UNIQUE Key in MySQL
Both PRIMARY KEY and UNIQUE constraints ensure that values in a column (or set of columns) are unique. However, they differ in rules, behavior, and intended use.
A table can have only one PRIMARY KEY, but it can have multiple UNIQUE keys.
PRIMARY KEY columns cannot contain NULL, while UNIQUE key columns can contain one NULL value (per column).
PRIMARY KEY is used to uniquely identify rows; UNIQUE keys enforce uniqueness but are not required for identification.
PRIMARY KEY automatically creates a clustered index (in InnoDB), while UNIQUE creates a secondary index.
Use PRIMARY KEY for the main identifier of the row.
Use UNIQUE when a column must be unique but is not the main identifier (e.g., email, username, phone number).
In summary, every PRIMARY KEY is unique, but not every UNIQUE key is a primary key. PRIMARY KEY identifies the row; UNIQUE simply prevents duplicates.
You need to add a column to a MySQL table that must be unique but also allow NULL values. How would you define it, and why would you choose a UNIQUE key over a PRIMARY key?
If you create a table with both a PRIMARY KEY and a UNIQUE key on the same column, what does MySQL enforce, and what happens if you try to insert duplicate values?
What happens if you try to define a PRIMARY key on a column that already has a UNIQUE index? Explain the effect on indexes.
During a code review you notice duplicate email addresses are being inserted despite a UNIQUE constraint. The table also has a PRIMARY KEY on id. Walk me through how you would debug this issue.
You are designing a user table that will be sharded later. Explain the trade‑offs of using a composite PRIMARY KEY versus a separate UNIQUE key for the email column.
Our application sometimes needs to insert rows without specifying the primary key value, relying on AUTO_INCREMENT. How does MySQL treat the UNIQUE constraint on another column in this scenario?
Our service experiences high write throughput and we notice contention on the primary key index. Would switching the email column from a UNIQUE key to a PRIMARY KEY help, and what are the performance implications?
We need to migrate a legacy table that currently uses a UNIQUE key as the natural identifier to a new schema where that column becomes the PRIMARY KEY. What steps and risks would you consider?
Explain how MySQL’s handling of NULLs differs between UNIQUE and PRIMARY keys, and how that impacts data integrity in a multi‑tenant SaaS database.
We are consolidating several micro‑services databases into a single MySQL cluster. How would you decide whether to keep existing UNIQUE constraints or promote them to PRIMARY keys to simplify cross‑service joins and replication?
In a long‑term roadmap, we plan to move from MySQL to a distributed SQL system that doesn’t support composite primary keys the same way. How would you redesign our schema’s UNIQUE vs PRIMARY key usage to minimize migration pain?
Discuss the operational trade‑offs of relying on PRIMARY keys for data deduplication versus using UNIQUE indexes, especially regarding backup/restore and point‑in‑time recovery.