Loading…
Loading…
Charting your lesson…
Opening your lesson…
Keys: Primary, Foreign, Candidate and Super Keys
Lesson 1 of 4
6 steps•about 12 min•12 flashcards
Step 1/ 61 min
Keys are how the relational model guarantees that every row is findable and that relationships between tables stay consistent. The family of key terms — super, candidate, primary, foreign — is a favourite exam area because the definitions nest precisely.
Core idea: a key is a set of attributes that identifies rows. A super key uniquely identifies a row (possibly with extra attributes); a candidate key is a minimal super key; the primary key is the chosen candidate key; a foreign key references another table's primary key to link tables.
Step 2/ 61 min
In a table Student(roll_no, email, name, dept), both roll_no and email uniquely identify a student. Any attribute set that guarantees uniqueness can serve as an identifier — that's the starting point.
{roll_no}, {roll_no, name}, {email, dept} are all super keys (the extras are redundant but don't break uniqueness).{roll_no} and {email} are candidate keys; {roll_no, name} is not (name is superfluous).Relationship: every candidate key is a super key; every primary key is a candidate key. (Candidate ⊆ Super.)
A foreign key is an attribute in one table that references the primary key of another (or the same) table, enforcing referential integrity: you cannot insert a value that doesn't exist in the referenced table.
CREATE TABLE Enrolment (
roll_no INT REFERENCES Student(roll_no), -- foreign key
course VARCHAR(10),
PRIMARY KEY (roll_no, course) -- composite primary key
);
A FK may be NULL (unlike a primary key) — meaning "no relationship yet". Delete/update behaviour (ON DELETE CASCADE, SET NULL, RESTRICT) governs what happens to child rows when a parent is removed. A composite key is any key made of multiple attributes.
Step 3/ 61 min
Table Employee(emp_id, ssn, email, name) where emp_id, ssn and email are each unique.
{emp_id}, {ssn}, {email} (each minimal and unique).{emp_id, name}, {ssn, email} — 14 in all (every attribute set containing at least one of emp_id, ssn, email).emp_id (stable, system-generated).ssn and email.Payroll has emp_id REFERENCES Employee(emp_id), that's a foreign key and you can't add a payroll row for a non-existent employee.Step 4/ 61 min
Guaranteed: "difference between super key, candidate key and primary key", "can a foreign key be NULL?" (yes; a primary key cannot), "what is referential integrity", and counting candidate/super keys given functional dependencies. GATE frequently asks "how many super keys" given the candidate keys.
Step 5/ 61 min
k attributes in an n-attribute table, the number of super keys containing it is 2^(n−k) (each other attribute is in or out).Calling any uniquely-identifying attribute set a "candidate key". A candidate key must be minimal — no proper subset is also unique. {roll_no, name} identifies a student but is only a super key, because {roll_no} alone already works. Forgetting the minimality condition conflates super keys with candidate keys.
Step 6/ 6· Worked examples2 min
Problem
Given the relation Student(roll_no, email, aadhaar, name, dept), where roll_no, email, and aadhaar are each unique and non-null, identify all candidate keys, give two super keys, choose a primary key with justification, and add a foreign key to Department(dept_id, ...).
Try it on paper first.
Recall
Keep this
Database keys are a DBMS interview staple — "difference between primary key and candidate key" and "what is a foreign key?" are near-guaranteed. Keys underpin integrity and normalization, so getting the hierarchy right is essential.
| Key | Unique | Minimal | NULL allowed |
|---|---|---|---|
| Super | yes | no | — |
| Candidate | yes | yes | no |
| Primary | yes | yes | no |
| Foreign | no | — | yes |
"Super key vs candidate key vs primary key", "can a foreign key be NULL?", "how many primary keys per table?", "what is referential integrity?", and identifying candidate keys from functional dependencies.
Super key (unique, maybe redundant) ⊃ candidate key (minimal unique) → primary key (one chosen, unique + NOT NULL); alternate = unused candidate. Foreign key references another table's primary key (can be NULL, enforces referential integrity). Composite key spans multiple columns.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.