skip to content

questions

3

In the relational model, what is the difference between a superkey, a candidate key, and a primary key?

level: juniorimportance: must knowfreq 68%

answer

  1. superkey = unique, candidate = unique AND minimal
  2. all attributes = always a superkey
  3. supersets of superkeys stay superkeys
  4. primary = elected candidate, non-null
  5. unelected candidates = alternate keys, still enforce them

basics

~20 s

A superkey is any set of attributes whose values are unique across all rows. A candidate key is a minimal superkey: drop any attribute and uniqueness breaks. The primary key is the one candidate key chosen as the official row identifier; the rest are alternate keys.

solid answer

~50 s

The three terms are nested. A **superkey** is any attribute set that determines the whole row, so no two rows share the same values for it. The full attribute list is always a superkey, and any superset of a superkey is also a superkey, so a relation usually has many. A **candidate key** is a superkey that is *minimal* (irreducible): remove any single attribute and uniqueness is lost. Minimality counts attributes, not bytes. A relation can have several candidate keys. For employees, employee number, national ID, and work email might each be one. The **primary key** is just the candidate key the designer elects as the canonical identifier, and it must be non-null. The remaining candidate keys are **alternate keys** and are still real keys that must be enforced, otherwise duplicates leak in through them. So every candidate key is a superkey and every primary key is a candidate key. Which candidate key becomes primary is a design decision, not a mathematical fact about the relation.

code

text · 6 lines
text
Employee(emp_id, national_id, email, dept_id, name)

superkeys      : {emp_id}, {emp_id, name}, {email, dept_id}, {all attributes}, ...
candidate keys : {emp_id}, {national_id}, {email}
primary key    : {emp_id}          (chosen by the designer, must be non-null)
alternate keys : {national_id}, {email}

go deeper

for a junior

Recall the nesting and the one-line definitions, and be able to point at candidate keys in a small three-or-four-column example.

for a middle

Explain minimality precisely, show that supersets of superkeys are superkeys, and connect the non-null rule on primary keys to entity integrity.

for a senior

Stress that keys encode business rules rather than current data, and that every candidate key you believe in must be enforced or it is not a key.

for a principal

Frame key choice as an interface decision: the primary key is the identity other systems and tables will depend on, so stability and disclosure risk matter as much as uniqueness.

## The single idea underneath the vocabulary A relation is a set of tuples, each describing one thing. To refer to "that row" you need a set of attributes whose value combination never repeats. Every key term is a refinement of that one question: which attribute combinations identify a row uniquely? Uniqueness here is a property of the **schema**, not of the rows that happen to be stored today. If two customers currently have different phone numbers, that does not make phone number a key. It is a key only if the business rule says two customers can never share one. Deriving keys from a data sample is one of the most common candidate mistakes. ## Superkey A set of attributes K is a superkey of relation R if, in every legal instance of R, no two distinct tuples agree on all of K. Equivalently, K functionally determines every attribute of R. Two consequences follow immediately: - The set of **all** attributes is always a superkey, because a relation is a set and sets contain no duplicate elements. - Any **superset** of a superkey is a superkey. Adding attributes can never create a collision that was not already there. That is why superkeys are cheap and numerous, and why the concept alone is not useful for design. ## Candidate key: superkey plus minimality A candidate key is a superkey with no proper subset that is also a superkey. This property is called minimality or irreducibility. For `Employee(emp_id, national_id, email, dept_id, name)` where employee number, national ID and email are each individually unique: - `{emp_id}`, `{national_id}`, `{email}` are candidate keys. - `{emp_id, name}` and `{email, dept_id}` are superkeys but not candidate keys: they contain a smaller superkey. - `{dept_id, name}` is neither, since a department can hold two people with the same name. Minimality is about the number of attributes, not their storage width. A four-byte integer superkey of two columns is not "more minimal" than a single wide text column that is already unique. Minimality also matters because a non-minimal key drags irrelevant attributes into every referencing table and into the definition of the normal forms. ## Primary key and alternate keys All candidate keys are logically equal: each one identifies rows just as well. The **primary key** is the one the designer nominates as the row's canonical identity, typically the one that is stable, narrow, always known, and convenient to reference from other relations. The relational model adds one rule to it, entity integrity: no attribute of the primary key may be null, because a row identified by an unknown value is not identified at all. The candidate keys not chosen become **alternate keys**. They are not decoration. If `email` is a real candidate key and you only enforce the primary key, the database will happily store two rows with the same email, and every downstream assumption about email identifying a person is now false. Each candidate key you believe in should be enforced as a uniqueness rule; each one you do not enforce is not actually a key, just a hope. ## Practical framing Interviewers ask this to check that you can reason about identity rather than recite syntax. Strong answers usually include: the nesting (superkey to candidate key to primary key), minimality as the discriminator, the fact that multiple candidate keys can coexist, the non-null rule on the primary key, and the reminder that keys come from business rules. A short worked example with three columns is faster and more convincing than a definition read out from memory.

  • Can one relation have more than one candidate key, and what do you do with the ones you do not choose as primary?
    Yes. Any relation can have several minimal unique attribute sets, for example employee number, national ID and work email on the same employee relation. The ones not elected are alternate keys and must still be enforced as uniqueness rules, otherwise the database will store rows that duplicate them. Only the primary key gets the extra non-null requirement and the role of default reference target.
  • Is the set of all attributes of a relation always a superkey?
    In the pure relational model yes, because a relation is a set of tuples so two identical tuples cannot both exist. It is usually not a candidate key, since some smaller subset is normally unique. Note that SQL tables are bags rather than sets, so a table without any uniqueness constraint can hold fully duplicate rows and then even the full column list is not unique.
  • How do you actually find the candidate keys of a relation?
    You start from the business rules or functional dependencies rather than from the stored rows. Take an attribute set that determines every attribute, then try removing attributes one at a time; if the remainder still determines everything, it was not minimal. Repeat until nothing can be removed, and explore alternative starting sets to find the other candidate keys.

Superkeys are every description that picks one person out of a room: "the tall woman in the red coat by the door". Candidate keys are the shortest such descriptions, with nothing removable. The primary key is the one description everyone agrees to use, like a staff number, while the other minimal descriptions stay valid.

saying these in an interview costs you the question

  • Using superkey and candidate key interchangeably, or defining candidate key as just "a unique column"
  • Claiming a primary key must be a single column or an auto-generated integer
  • Reading minimality as "smallest in bytes" or "most efficient" instead of "no removable attribute"
  • Deriving keys from the rows currently stored rather than from declared business rules
  • Saying alternate keys are optional or purely documentation, so they need no uniqueness enforcement

context

open as a page

What is a composite key, and what makes an attribute 'prime' rather than 'non-prime' in a relation?

level: middleimportance: should knowfreq 42%

basics

~20 s

A composite key is a candidate key made of two or more attributes that are only unique together. A prime attribute is one that belongs to at least one candidate key of the relation; every other attribute is non-prime. Prime status is per relation, not per column type.

open as a page

Does a foreign key have to point at the parent table's primary key? Explain what a foreign key actually requires of the attributes it references.

level: seniorimportance: should knowfreq 34%

basics

~20 s

No. A foreign key must reference a candidate key of the parent, meaning a declared unique, minimal attribute set. The primary key is the usual target because it is stable and non-null, but any enforced candidate key works. Referencing non-unique attributes is meaningless because the reference would not denote one row.

open as a page