Finding All Candidate Keys of a Relation
A reliable method for finding every candidate key: classify attributes by where they appear in the dependencies, build a compulsory core, then extend it only as far as necessary.
-
DBMS Fundamentals
- Data, Information and Databases
- What a DBMS Is and Why It Exists
- File System versus DBMS
- Advantages and Limitations of a DBMS
- Database Users and the Role of the DBA
- Three Level Architecture and Data Abstraction
- Logical and Physical Data Independence
- Schema, Instance and Metadata
- Database Applications and the Database System Environment
- Database Architecture
- Data Models
-
ER Model
- Entities, Entity Sets and Entity Types
- Types of Attributes in the ER Model
- Keys in the ER Model
- Relationships, Relationship Sets and Degree
- Cardinality and Participation Constraints
- Strong and Weak Entities
- Drawing and Reading ER Diagrams
- Extended ER: Generalisation, Specialisation and Aggregation
- Converting an ER Diagram into Relational Tables
- ER Design Projects
- Relational Model
- Relational Algebra
- Functional Dependencies
-
Normalisation
- Why Normalisation Exists: Anomalies and Redundancy
- First Normal Form
- Second Normal Form and Partial Dependency
- Third Normal Form and Transitive Dependency
- BCNF and BCNF Decomposition
- 4NF, 5NF, Multivalued and Join Dependencies
- Lossless Decomposition and Dependency Preservation
- Complete Worked Normalisation: Unnormalised to BCNF
- Denormalisation and When to Use It
Concept
Finding candidate keys is the most frequently set numerical question in this subject. Trial and error works on tiny examples and fails on real ones. The method below is systematic and always terminates.
Step 1 — classify every attribute
| Class | Appears | Rule |
|---|---|---|
| Essential | Only on the left, or in no dependency at all | Must be in every candidate key |
| Non essential | Only on the right | Never in any candidate key |
| Middle | On both sides | May or may not be needed — test it |
The reasoning behind row one: if an attribute never appears on the right, nothing can determine it, so the only way to know its value is to include it in the key. The reasoning behind row two follows the same logic in reverse — an attribute that is always determined by something else is never needed in a minimal key.
Step 2 — the procedure
1 Classify all attributes as essential, non essential
or middle.
2 Let CORE = the essential attributes.
Compute CORE+.
if CORE+ = R then CORE is the ONLY candidate key.
STOP - nothing else can be minimal.
3 Otherwise, add ONE middle attribute to CORE and
compute the closure of each combination.
any combination whose closure is R is a candidate key.
4 If any combination still falls short, try CORE plus
TWO middle attributes, and so on.
5 Never extend a set that is already a candidate key -
the result would not be minimal.Worked example 1 — a single key
R ( A, B, C, D )
F = { A -> B, B -> C, C -> D }
STEP 1 classify
left sides: A, B, C
right sides: B, C, D
A left only -> ESSENTIAL
B both -> middle
C both -> middle
D right only -> NON ESSENTIAL
STEP 2 CORE = { A }
A+ : { A }
A -> B { A, B }
B -> C { A, B, C }
C -> D { A, B, C, D }
A+ = R
-> { A } is the ONLY candidate key. Stop.Worked example 2 — two keys
R ( A, B, C, D, E )
F = { A B -> C, C -> D, D -> A, B D -> E }
STEP 1 classify
left sides: A, B, C, D
right sides: C, D, A, E
A both -> middle
B left only -> ESSENTIAL
C both -> middle
D both -> middle
E right only -> NON ESSENTIAL
STEP 2 CORE = { B }
B+ = { B } no dependency has B alone on the left
not R, so continue
STEP 3 add one middle attribute at a time
(B A)+ : { A, B }
A B -> C { A, B, C }
C -> D { A, B, C, D }
D -> A have A
B D -> E { A, B, C, D, E }
= R -> CANDIDATE KEY { A, B }
(B C)+ : { B, C }
C -> D { B, C, D }
D -> A { A, B, C, D }
A B -> C have C
B D -> E { A, B, C, D, E }
= R -> CANDIDATE KEY { B, C }
(B D)+ : { B, D }
D -> A { A, B, D }
A B -> C { A, B, C, D }
B D -> E { A, B, C, D, E }
= R -> CANDIDATE KEY { B, D }
STEP 4 do NOT test { A, B, C } or larger - each already
contains a candidate key, so none can be minimal.
ANSWER three candidate keys: { A, B }, { B, C }, { B, D }
Note B is in all three, exactly as step 1 predicted.Worked example 3 — no essential attribute
R ( A, B, C )
F = { A -> B, B -> C, C -> A }
STEP 1 every attribute appears on both sides
-> no essential attributes, CORE is empty
STEP 2 empty closure is empty. Continue.
STEP 3 try each single attribute
A+ = { A, B, C } = R -> candidate key
B+ = { B, C, A } = R -> candidate key
C+ = { C, A, B } = R -> candidate key
ANSWER { A }, { B }, { C } - three candidate keys.
A cycle of dependencies makes every attribute a key on
its own, which is a useful pattern to recognise on sight.Prime and non prime attributes
| Term | Definition | In example 2 |
|---|---|---|
| Prime | Belongs to at least one candidate key | A, B, C, D |
| Non prime | Belongs to no candidate key | E |
These terms are needed from the very first sentence of Phase 8: 2NF and 3NF are both defined in terms of non prime attributes, so candidate keys must be found before normalisation can even begin.
Common mistakes
- Skipping the classification step. It removes most of the search space in one move.
- Including a right only attribute in a key. It can never be needed.
- Stopping at the first candidate key. The question usually asks for all of them.
- Testing supersets of a known candidate key. Those cannot be minimal.
- Forgetting an attribute that appears in no dependency. It is essential and must be in every key — a classic trap.
- Confusing prime with primary. Prime means in some candidate key.
Exam and interview questions
- Describe the method for finding all candidate keys.
- Why must an attribute appearing only on the left be in every candidate key?
- Why can an attribute appearing only on the right never be in a candidate key?
- Define prime and non prime attributes.
- What does it tell you when the closure of the essential attributes is already the whole relation?
Practice
- R(A,B,C,D,E), F = { A -> B C, C D -> E, B -> D, E -> A }. Find all candidate keys.
- R(A,B,C,D), F = { A B -> C, C -> D, D -> A }. Find all candidate keys and list prime attributes.
- R(A,B,C,D,E,F), F = { A B -> C, C -> D E, E -> F }. Find the candidate key and explain why it is unique.
- Construct a relation with exactly four candidate keys.
Conclusion
Classify attributes first, build the compulsory core, then extend one attribute at a time and stop as soon as a set is minimal. The prime and non prime labels that fall out of this are what Phase 8 needs to define 2NF and 3NF.