5.4 Algorithmic Method to Find All Candidate Keys
π‘ Core Intuitionβ
π³ The Everyday Analogy: The Detective's Clue Matrixβ
Imagine a detective solving a complex mystery involving 6 suspects:
- Some pieces of evidence are "Orphans" (Essential Attributes): Nobody can explain where they came from; they have zero leads pointing to them. If you don't collect them manually, they will never be accounted for. They must be in your case file.
- Some pieces of evidence are "Consequences" (Right-Only Attributes): They are purely the side-effects of other clues. You never need to start your investigation with them because they will automatically be discovered along the way.
- By identifying the essential clues first, you drastically prune a search space from thousands of random combinations down to two or three targeted leads.
π» Bridging to Computer Scienceβ
Finding all candidate keys of a relation schema by testing all subsets of attributes is computationally intractable for large tables. The Graphical In-Degree / Essential Attribute Method classifies attributes into structural categories, pruning over 90% of impossible candidate key permutations instantly.
π Core Deep-Dive & Conceptsβ
1. The 4-Quadrant Attribute Classification Methodβ
Given a relation schema and its functional dependency set , we inspect where each attribute appears across all dependencies :
Category 1: Left-Side Only Attributes (Essential Attributes)β
Attributes that appear exclusively on the Left-Hand Side (Determinant) of FDs and never on the Right-Hand Side.
- The Law: Since no functional dependency in the system can ever derive these attributes, they MUST be included in every single Candidate Key of the relation.
Category 2: Right-Side Only Attributesβ
Attributes that appear exclusively on the Right-Hand Side (Dependent) of FDs and never on the Left-Hand Side.
- The Law: These attributes are determined by other attributes but cannot determine anything themselves. Therefore, they can NEVER be part of any minimal Candidate Key.
Category 3: Both-Side Attributes (Intermediate Attributes)β
Attributes that appear on both the LHS of some FDs and the RHS of other FDs.
- The Law: They may or may not be part of a candidate key. They are systematically combined with the essential attributes during search space exploration.
Category 4: Neither-Side Attributes (Isolated Attributes)β
Attributes of relation schema that do not appear in any functional dependency in .
- The Law: Since no dependency determines them, they MUST be included in every single Candidate Key of .
2. Systematic Algorithmic Workflowβ
Algorithm: Find_All_Candidate_Keys(R, F)
1. Categorize all attributes into Essential (L), Right-Only (R), Both (B), and Isolated (I).
2. Form the Base Essential Set: K_base = L βͺ I.
3. Compute closure of Base Set: (K_base)+.
4. If (K_base)+ == R:
K_base is the ONLY Candidate Key of R. Terminate.
5. If (K_base)+ != R:
Iteratively pair K_base with 1 attribute from Both-Side (B):
For each attribute b β B:
Compute (K_base βͺ {b})+.
If closure equals R, (K_base βͺ {b}) is a Candidate Key.
If keys are found at this level, test higher-order combinations
only if they do not contain an already confirmed Candidate Key (minimality check).
3. Fully Solved Step-by-Step Numerical Problemsβ
Problem 1: Single Candidate Key with In-Degree Pruningβ
Consider relation with functional dependency set:
Goal: Find all Candidate Keys of .
Step-by-Step Derivation:
- Step 1: Attribute Classification:
- Attributes on LHS:
- Attributes on RHS:
- Left-Only (Essential Attributes): Attributes , and have zero incoming edges (they never appear on the right side).
- Therefore, the attribute set must be part of every candidate key.
- Step 2: Test Closure of Essential Attributes:
- Using
- Using
- Using
- Using
- Step 3: Minimality Test: Since are all essential attributes, no proper subset can ever derive all attributes (as the missing essential attribute could never be derived).
Final Conclusion:
There is only one Candidate Key.
Problem 2: Multiple Overlapping & Varying-Length Candidate Keysβ
Consider relation with functional dependency set:
Goal: Find all Candidate Keys, Prime Attributes, and Non-Prime Attributes.
Step-by-Step Derivation:
-
Step 1: Attribute Classification:
- Attributes on LHS:
- Attributes on RHS:
- Essential Attributes (Left-Only):
Attribute appears on the LHS () but never on the RHS.
Therefore, must be present in EVERY candidate key of this relation! - Right-Only Attributes: Attribute appears on RHS only (). will never be part of any minimal candidate key.
- Both-Side Attributes: .
-
Step 2: Test Closure of Essential Set :
alone cannot form a candidate key.
-
Step 3: Test 2-Attribute Combinations with :
- Test :
- is a valid Candidate Key!
- Test :
- (No dependencies fire).
- Test :
- .
- Test :
- is a valid Candidate Key!
- Test :
-
Step 4: Test 3-Attribute Combinations with :
- We only test combinations containing and subsets of that do not contain existing keys ( or ):
- Test :
- Minimality check: Is any proper subset a superkey?
- , , (lacks essential ).
- is a valid Candidate Key!
Summary of Keys:
Prime Attributes (members of at least one candidate key):
Non-Prime Attributes:
π Architecture / Visual Blueprintβ
π In The Real World: Production Case Studyβ
Distributed Shard Key Selection in CockroachDB & Cassandraβ
In horizontally scaled distributed relational databases:
- The Engineering Constraint: Tables are partitioned (sharded) across hundreds of cloud nodes using a primary distribution key.
- The Hazard: Choosing an attribute set that is a Super Key but not minimal leads to massive network overhead and unbalanced hotspot partitions.
- The Algorithm in Practice: Automated database schema linters parse all domain access patterns and functional constraints to calculate the minimal candidate keys. The database architect picks the candidate key with the highest cardinality and lowest write skew to act as the cluster's primary distribution shard key.
π― Exam & Interview Pitfall Checkβ
Question 1: Why is an attribute that never appears in any functional dependency guaranteed to be present in every Candidate Key?
Answer:
If an attribute does not appear in any functional dependency in , no dependency can ever functionally determine ( where ). The only way for an attribute closure to include is if is explicitly supplied in the starting set . Since a candidate key must derive all attributes of the relation schema, every candidate key must contain .
Question 2: Given with . Find all Candidate Keys.
Answer:
All three attributes appear on both the LHS and RHS (no essential attributes, no right-only attributes).
- Test : . is a Candidate Key.
- Test : . is a Candidate Key.
- Test : . is a Candidate Key.
Candidate Keys: . (All attributes are Prime; zero non-prime attributes).
Trap 1: "Can a Candidate Key have more attributes than another Candidate Key in the same table?"
Answer: Yes! Minimality does not mean minimum cardinality (fewest number of attributes). Minimality means irreducibilityβno proper subset is a superkey. As demonstrated in Problem 2, has candidate keys of length 2 () and length 3 () simultaneously.
Trap 2: "If an attribute appears on both LHS and RHS, can it be part of a candidate key?"
Answer: Absolutely. Both-side attributes frequently appear in candidate keys. For instance, in , all three attributes appear on both sides, and all three are individual candidate keys.