What is the difference between primary, foreign, and unique keys?
This tests relational integrity basics. Answer: primary keys identify rows, foreign keys reference tables, and unique keys are alternate candidates. Red flag: saying unique keys are just for indexing or omitting a non-PK example like email.
What's really being asked
This question probes whether you understand relational integrity constraints as distinct concepts rather than interchangeable database keywords. Interviewers want to see that you can articulate why each constraint exists, what specific problem it solves, and how they interact inside a real schema. At the senior level, this is foundational; confusion here signals weak data modeling instincts and a shallow understanding of normalization.
The full answer
A good answer hits four things in order. First, define a primary key as the single chosen candidate key that uniquely identifies every row in a table and typically does not change over the life of the record. Second, define a foreign key as a column or set of columns that establishes a link between two tables by referencing the primary key of another table, thereby enforcing referential integrity and preventing orphaned records. Third, define a unique key as an alternate candidate key that also guarantees uniqueness but was not selected as the primary key; it prevents duplicate values across one or more columns without serving as the main join target. Fourth, describe a concrete scenario where a unique key is applied to a non-primary column, such as an email address or government ID, because the business requires uniqueness but the surrogate primary key remains an auto-incrementing integer for performance and stability.
The mistakes people make
Common wrong answers include stating that unique keys are merely indexes created for query performance, conflating unique constraints with primary keys, or describing foreign keys as a type of unique key. Another red flag is failing to mention that unique keys can span multiple columns or omitting the concept of candidate keys entirely. Some candidates also incorrectly claim that a table must have a primary key before it can have a unique key, or that foreign keys automatically enforce uniqueness on the referencing side rather than just the referenced side.
What usually comes next
Interviewers often follow up by asking how NULL values interact with unique constraints, whether a table can have multiple unique keys, or the performance implications of adding unique constraints. They may also ask you to compare a unique index versus a unique constraint, or to sketch a schema where a join table uses composite unique keys to prevent duplicate many-to-many relationships while still allowing fast lookups.
A concrete example
Consider a users table in a SaaS application. The primary key is a bigint auto-incrementing user_id, chosen for stable joins and compact storage. However, the business requires that every account have a distinct email address. You place a unique constraint on the email column so the database rejects duplicate signups at the storage layer. The email is an alternate key, it uniquely identifies a user for business purposes, but it is not the primary key because email addresses can change and are less efficient as join targets than integer IDs. If a user updates their email, the primary key remains stable and all foreign key relationships stay intact.
Interview question
What is the main reason to place a unique constraint on a non-primary column such as email?
- a.To prevent NULL values from being inserted into that column
- b.To create a performance index for faster lookups on that column
- c.To enforce business-level uniqueness while keeping a stable primary key for joinsCorrect
- d.To serve as the main row identifier that other tables use for relationships
Why? this is the answer
The card gives email as an alternate key that stops duplicate signups while the auto-incrementing primary key stays the stable join target. The distractor that it serves as the main row identifier conflates a unique constraint with a primary key, a red flag the card explicitly warns against.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
- #databases
- #sql
- #constraints
- #data-modeling
- #relational-model
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on databases — each one lists the topics its interview covers.
See open roles