Quick answer: A candidate key is a minimal set of attributes that identifies every row of a table. One candidate key is chosen as the primary key (unique and never null), the others are alternate keys, and any superset of a candidate key is a super key. A foreign key must match an existing key value in the referenced table or be null. If a relation has n attributes and a single one-attribute candidate key, it has super keys.
GATE often tests key counting and referential actions, because both are quick to ask and easy to get wrong. This topic is part of the ER Model and Relational Model chapter.
Basic relational terminology
| Term | Meaning |
|---|---|
| Relation | A table |
| Tuple | A row (record) |
| Attribute | A column (field) |
| Degree (arity) | Number of attributes |
| Cardinality | Number of tuples |
| Domain | Set of allowed values for an attribute |
In the relational model a relation is a set of tuples, so no two rows can be identical. That is why every relation has at least one key.
Types of keys
| Key | Definition |
|---|---|
| Super key | Any set of attributes that identifies rows uniquely (a superset of some candidate key) |
| Candidate key | A minimal super key: no proper subset is a super key |
| Simple candidate key | A candidate key with one attribute |
| Composite (compound) key | A candidate key with two or more attributes |
| Primary key | The candidate key chosen by the designer; unique and not null; at most one per table |
| Alternate key | Any candidate key that is not the primary key |
| Foreign key | Attributes that refer to the primary key (or another unique key) of the same or another table |
Prime and non-prime attributes
- Prime attribute: belongs to at least one candidate key.
- Non-prime attribute: belongs to no candidate key.
Example: R(A, B, C, D, E) has candidate keys {A, B} and {B, C}. Both keys are composite and they overlap on B. Prime attributes are A, B and C. Non-prime attributes are D and E.
How to count super keys
A set is a super key if it contains at least one candidate key. For a relation with n attributes:
| Candidate keys | Number of super keys |
|---|---|
| {A1} | |
| {A1, A2} (one composite key) | |
| {A1} and {A2} | |
| {A1, A2} and {A3, A4} | |
| Every single attribute is a candidate key | |
| Only the full set of n attributes is a key | 1 |
The rule behind every row is inclusion–exclusion: add the supersets of each candidate key, subtract the supersets of each pair's union, add back the triples, and so on. A superset of a key K in an n-attribute relation can freely include or leave out each of the other attributes, giving choices.
Worked example 1
R(A, B, C, D) with candidate key {A}.
Step 1: The other 3 attributes can each be in or out.
So R has 8 super keys.
Worked example 2
R(A, B, C, D) with candidate keys {A} and {B, C}.
Step 1: Supersets of {A}:
Step 2: Supersets of {B, C}:
Step 3: Supersets of the union {A, B, C}, counted twice above:
Step 4: Apply inclusion–exclusion:
So R has 10 super keys.
Entity integrity
The primary key cannot be null, because a null value cannot identify a row. In SQL, a PRIMARY KEY behaves like UNIQUE plus NOT NULL.
Referential integrity
The referenced (parent) table holds the key. The referencing (child) table holds the foreign key.
Example: Student(Sid, Sname, Age) is the parent. Enroll(Sid, Cid, Fee) is the child, and Enroll.Sid references Student.Sid.
Which operations can break referential integrity?
| Operation | On the parent table | On the child table |
|---|---|---|
| Insert | Never | Can (a foreign key value with no matching parent) |
| Delete | Can (child rows lose their parent) | Never |
| Update | Can (if the key value changes) | Can (if the foreign key changes) |
An insert or update on the child that breaks the rule is rejected.
Referential actions when a parent row is deleted or updated
| Action | Effect |
|---|---|
| NO ACTION / RESTRICT | Reject the delete or update if any child row refers to the parent |
| CASCADE | Delete (or update) the matching child rows too |
| SET NULL | Set the child's foreign key to null |
| SET DEFAULT | Set the child's foreign key to its default value |
Note: SET NULL needs a nullable foreign key. If the column is NOT NULL, the action cannot succeed and the delete is rejected.
Worked example: cascading delete in a self-referencing table
Table T(A, B): A is the primary key, B is a foreign key referencing A, with ON DELETE CASCADE.
| A | B |
|---|---|
| 2 | 4 |
| 3 | 4 |
| 4 | 5 |
| 5 | 4 |
| 6 | 2 |
Step 1: Delete (2, 4).
Step 2: Row (6, 2) has B = 2, so it is deleted as well.
Step 3: No row has B = 6, so the cascade stops.
Remaining rows: (3, 4), (4, 5), (5, 4).
Domain and other constraints
- Domain constraint: every value must come from the attribute's domain (type and range).
- NOT NULL, UNIQUE and CHECK restrict values in individual columns.
Summary table
| Fact | Value |
|---|---|
| Primary keys per table | At most 1 |
| Candidate keys per table | At least 1 |
| PRIMARY KEY in SQL | UNIQUE + NOT NULL |
| Super keys, one single-attribute key | |
| Super keys, every attribute a key | |
| Operations that can break referential integrity | Parent delete/update, child insert/update |
Exam tips
- To count super keys with two or more candidate keys, always use inclusion–exclusion on the union of keys, never on the keys themselves.
- When tracing a cascade, keep a list of deleted key values and keep scanning until a full pass deletes nothing new.
- Prime attributes come from all candidate keys, not just the primary key.
Common mistakes
- Calling a non-minimal set a candidate key. Check that no proper subset is also a key.
- Using instead of when every attribute is a key. The empty set is not a key.
- Thinking an insert into the parent can break referential integrity. It can't.
- Stopping a cascade too early. Follow every chain of references.
Practice questions
Q1. R(A, B, C, D, E) has the single candidate key {A, B}. How many super keys does it have?
Answer: .
Q2. R(A, B, C) has candidate keys {A}, {B} and {C}. How many super keys?
Answer: .
Q3. In the cascade example above, which rows remain if (4, 5) is deleted instead?
Answer: Rows with B = 4, namely (2, 4), (3, 4) and (5, 4), are deleted. Then (6, 2) goes because 2 was deleted. The table ends up empty.
FAQs
What is the difference between a super key and a candidate key?
A super key is any set of attributes that identifies rows uniquely. A candidate key is a super key with no extra attribute, so removing any attribute from it breaks uniqueness.
Can a foreign key be null?
Yes, unless the column is declared NOT NULL. A null foreign key means the row does not refer to any parent row.
Can a foreign key refer to the same table?
Yes. A self-referencing foreign key, such as manager_id in an Employee table pointing to another employee, is common.
How many primary keys can a table have?
Only one. A table can have many candidate keys, but just one is chosen as the primary key.
How many super keys does a relation with n attributes have if every attribute is a candidate key?
It has 2ⁿ − 1 super keys, because every non-empty subset of attributes contains a key.
Conclusion
Practise the super key formulas and trace cascades step by step; both show up regularly in GATE. Next, see how ER diagrams turn into tables in ER diagram to relational tables, or go back to the ER Model and Relational Model chapter. Finding candidate keys from functional dependencies is covered in functional dependencies.