Explain the difference between a superkey, a candidate key and a primary key in the relational model, and what makes an attribute 'prime'.
answer
- Superkey determines everything
- Candidate key = superkey that is minimal
- Primary key = one nominated candidate key
- Prime = member of some candidate key
- Attribute never on a right-hand side must be in every key
basics
~20 sA superkey is any column set that determines all other columns. A candidate key is a minimal superkey - remove any column and it stops being one. The primary key is the candidate key you designate. Prime attributes are those inside some candidate key.
solid answer
~50 sGiven a relation R with functional dependencies F: - **Superkey**: a set X of attributes with `X -> R`, i.e. X determines every attribute. The full attribute list is always a superkey, so superkeys are cheap. - **Candidate key**: a superkey that is *irreducible* - no proper subset of it is still a superkey. Minimality is the whole distinction. - **Primary key**: one candidate key chosen by the designer as the identifying key. The others are alternate keys; nothing in the theory makes the primary one special. - **Prime attribute**: an attribute that belongs to at least one candidate key. Everything else is non-prime. A relation can have several candidate keys, and they may overlap. In `enrollment(student_id, course_id, seat_no, term)` with one seat per student per term, both `(student_id, term)` and `(seat_no, term)` may be candidate keys, making `student_id`, `term` and `seat_no` all prime. The prime/non-prime split matters because the normal forms are phrased in terms of it.
code
sql · 6 linesCREATE TABLE employee (
emp_id bigint PRIMARY KEY,
national_id varchar(20) NOT NULL UNIQUE,
email varchar(255) NOT NULL UNIQUE,
full_name varchar(200) NOT NULL
);go deeper
Give the four definitions crisply with one example each, and stress that minimality is what separates candidate key from superkey.
Show how to derive candidate keys from an FD set with closures, and handle a relation with two overlapping candidate keys.
Point out that alternate keys must still be enforced, and connect the prime/non-prime split to why key derivation must come before any normalization judgement.
Discuss the consequences of an incorrectly asserted key - duplicate business entities, broken references, migrations that cannot be undone - and how key claims should be reviewed against domain rules.
## Superkey A **superkey** of relation R is any attribute set X such that the functional dependency `X -> R` holds - X determines the value of every attribute in the relation, so no two distinct rows can share the same X value. The set of all attributes is trivially a superkey (two rows equal on everything are the same row under set semantics). Any superset of a superkey is again a superkey, so a relation typically has many. Operationally you test a superkey with attribute closure: compute X+ under the FD set and check whether it contains every attribute of R. ## Candidate key A **candidate key** is a *minimal* superkey: X determines all attributes, and for every attribute a in X, `X - {a}` no longer does. Minimality is the only difference from a superkey, and it is the part candidates forget. If `(student_id, course_id)` is a candidate key of an enrollment relation, then `(student_id, course_id, enrolled_on)` is a superkey but not a candidate key, because dropping `enrolled_on` still identifies rows. A relation may have **several** candidate keys. Consider `employee(emp_id, national_id, email, name)` where all three of `emp_id`, `national_id` and `email` are individually unique: there are three candidate keys, each a single attribute. Candidate keys may also overlap partially, sharing some attributes without either containing the other. The empty set can even be a candidate key, in the degenerate case of a relation constrained to hold at most one row. ## Primary key and alternate keys The **primary key** is whichever candidate key the designer nominates as the official identifier. The remaining candidate keys are **alternate keys** and normally get their own uniqueness constraints. Relational theory attaches no special power to the primary key; the choice is a design decision about stability, width and what other tables will reference. What theory does insist on is that all candidate keys are enforced, not just the nominated one - a very common practical omission, where a surrogate identifier is declared primary and the genuine business uniqueness is left unconstrained, allowing duplicates. ## Prime and non-prime attributes An attribute is **prime** if it is a member of at least one candidate key; otherwise it is **non-prime**. Note the quantifier: membership in *some* key suffices, not all of them. In `enrollment(student_id, course_id, term, seat_no, grade)` where `(student_id, term)` and `(seat_no, term)` are both candidate keys, the prime attributes are `student_id`, `term` and `seat_no`; `course_id` and `grade` are non-prime. This classification exists because the higher normal forms are stated with it: the rules that restrict dependencies of non-prime attributes on parts of keys, and the rule that allows certain dependencies only when the determined attribute is prime, both hinge on this label. Get the candidate keys wrong and every subsequent normalization judgement is wrong too. ## Working it out The practical procedure is: 1. Classify attributes by their appearance in the FD set. An attribute that never appears on any right-hand side must be in **every** candidate key - nothing can derive it. An attribute that appears only on right-hand sides can be in none. 2. Take the mandatory attributes as a seed and compute their closure. If it is already all of R, that seed is the unique candidate key and you are done. 3. Otherwise extend the seed with one additional attribute at a time, computing closures, and keep the sets that reach all of R. Discard any that contain a smaller solution, since candidate keys must be minimal. ## Keys versus their implementation A candidate key is a logical property implied by the FDs. A `PRIMARY KEY` or `UNIQUE` declaration is the mechanism that makes the engine reject violations, and an index is the structure that usually enforces it. The three are different levels: a key exists in the design even if nobody declared it, and declaring a uniqueness constraint that does not correspond to a real domain rule breaks legitimate data. Which candidate key is nominated as primary, and whether it is a business attribute or a generated one, is a separate design conversation from identifying what the candidate keys are.
- Can a relation have more than one candidate key, and what do you do with the ones you do not choose as primary?Yes - any relation whose FDs give several minimal determinant sets has multiple candidate keys. The ones not nominated are alternate keys and should each carry a uniqueness constraint. Leaving them unenforced is a frequent defect: the table accepts duplicate business identities even though the design says they are unique.
- How do you show that a given attribute set is a candidate key rather than just a superkey?Compute the attribute closure of the set under the FDs and confirm it contains every attribute, which proves it is a superkey. Then remove each attribute in turn and recompute the closure; if every reduced set fails to cover all attributes, the original set is minimal and therefore a candidate key.
saying these in an interview costs you the question
- Using superkey and candidate key interchangeably, ignoring minimality
- Assuming a relation has exactly one candidate key
- Calling an attribute prime only if it is in the primary key rather than in any candidate key
- Believing the primary key has special theoretical status beyond being a nominated candidate key
- Declaring a generated identifier primary and leaving the real business uniqueness unconstrained