Candidate Keys, Superkeys and Key Identification
Understand superkeys, candidate keys, primary keys, alternate keys, composite keys, and how to identify candidate keys using functional dependencies and attribute closure.
1. Introduction
In a relational database, every record should be identifiable so that we can retrieve, update, or delete the correct record without confusion.
For example, a university may store thousands of student records. If two students have the same name, identifying a student only by their name becomes unreliable.
To solve this problem, databases use keys.
In this article, we will understand superkeys, candidate keys, primary keys, and how to identify candidate keys using functional dependencies and attribute closure.
2. What Is a Superkey?
A superkey is an attribute or a set of attributes that can uniquely identify every tuple (row) in a relation.
A superkey may contain additional attributes that are not necessary for identification.
Example
Consider the following Student relation:
| Student_ID | Name | Dept_ID | |
|---|---|---|---|
| 101 | Aamir | aamir@mail.com | D01 |
| 102 | Sara | sara@mail.com | D02 |
| 103 | Umar | umar@mail.com | D01 |
Assume that Student_ID and Email are both unique for every student.
Therefore, the following are superkeys:
- Student_ID
- (Student_ID, Name)
- (Student_ID, Dept_ID)
- (Email, Name)
- (Student_ID, Email)
Each of these attribute sets can uniquely identify a student.
However, some of them contain unnecessary attributes.
For example, Student_ID alone is sufficient to identify a student. Therefore, adding Name to Student_ID does not improve its identification capability.
This is the reason we distinguish superkeys from candidate keys.
3. What Is a Candidate Key?
A candidate key is a minimal superkey.
It is an attribute or a set of attributes that uniquely identifies every tuple in a relation, and no attribute can be removed from it without losing this ability.
Example
In the Student relation above:
- Student_ID uniquely identifies students.
- Email uniquely identifies students.
- (Student_ID, Name) also uniquely identifies students.
But Student_ID and Email are minimal, whereas (Student_ID, Name) is not.
Therefore:
\[
\text{Candidate Keys} =
{Student_ID,\ Email}
\]
The combination (Student_ID, Name) is a superkey, but not a candidate key.
Properties of Candidate Keys
- A relation may have multiple candidate keys.
- A candidate key must uniquely identify each tuple.
- A candidate key must be minimal.
- Candidate keys cannot contain unnecessary attributes.
4. Superkey vs Candidate Key
| Superkey | Candidate Key |
|---|---|
| Uniquely identifies tuples | Uniquely identifies tuples |
| May contain unnecessary attributes | Contains no unnecessary attributes |
| May contain multiple attributes | May contain one or multiple attributes |
| Every candidate key is a superkey | Every superkey is not necessarily a candidate key |
The key relationship can be summarized as:
\[
\boxed{\text{Candidate Key} \subseteq \text{Superkeys}}
\]
Every candidate key is a superkey, but not every superkey is a candidate key.
5. What Is a Primary Key?
A primary key is the candidate key selected by the database designer to uniquely identify records in a relation.
If a table has multiple candidate keys, one of them is chosen as the primary key.
The remaining candidate keys are called alternate keys.
Example
Suppose a Student table has two candidate keys:
- Student_ID
The designer selects Student_ID as the primary key.
| Student_ID | Name | |
|---|---|---|
| 101 | Aamir | aamir@mail.com |
| 102 | Sara | sara@mail.com |
Here:
- Student_ID → Primary Key
- Email → Alternate Key
A primary key must satisfy uniqueness and entity integrity: it cannot contain duplicate values or NULL values.
6. Composite Keys
Sometimes a single attribute cannot uniquely identify a record. In such cases, two or more attributes are combined to form a key.
This is called a composite key.
Example: Student Course Enrollment
| Student_ID | Course_ID | Semester | Grade |
|---|---|---|---|
| 101 | CS101 | 1 | A |
| 101 | CS102 | 1 | B |
| 102 | CS101 | 1 | A |
Student_ID alone cannot identify an enrollment because one student can enroll in multiple courses.
Course_ID alone cannot identify an enrollment because many students can enroll in the same course.
However, the combination of Student_ID and Course_ID can identify a student’s enrollment in a course, assuming each student can enroll in a particular course only once.
Therefore:
\[
(Student_ID, Course_ID) \rightarrow Grade
\]
The combination:
\[
\boxed{(Student_ID, Course_ID)}
\]
is a candidate key for this relation, assuming the stated uniqueness rule.
A composite key is not necessarily a candidate key automatically. It must still satisfy uniqueness and minimality.
7. Finding Candidate Keys Using Attribute Closure
We can use attribute closure to identify candidate keys.
Important Rule
If the closure of an attribute set contains all attributes of a relation, that attribute set is a superkey.
If it is also minimal, it is a candidate key.
Example 1: Finding a Candidate Key
Consider the relation:
\[
R(A,B,C,D)
\]
Given the functional dependencies:
\[
F = \{
\begin{aligned}
A &\rightarrow B \\
B &\rightarrow C \\
C &\rightarrow D
\end{aligned}
\}
\]
Find the candidate key.
Step 1: Calculate \(A^+\)
Start with:
\[
A^+ = {A}
\]
Using \(A \rightarrow B\):
\[
A^+ = {A,B}
\]
Using \(B \rightarrow C\):
\[
A^+ = {A,B,C}
\]
Using \(C \rightarrow D\):
\[
A^+ = {A,B,C,D}
\]
Therefore:
\[
A^+ = R
\]
A determines every attribute in the relation.
Hence A is a superkey.
Since A is a single attribute and has no proper non-empty subset to test, it is minimal.
Therefore:
\[
\boxed{A \text{ is a Candidate Key}}
\]
8. Finding Composite Candidate Keys
Consider another relation:
\[
R(A,B,C,D)
\]
Given:
\[
F = \{
\begin{aligned}
A &\rightarrow C \\
B &\rightarrow D \\
CD &\rightarrow A
\end{aligned}
\}
\]
Find the candidate keys.
Step 1: Calculate \(A^+\)
Start:
\[
A^+ = {A}
\]
Using \(A \rightarrow C\):
\[
A^+ = {A,C}
\]
No further dependency can be applied.
Therefore:
\[
A^+ = {A,C}
\]
A alone is not a superkey because it does not determine B or D.
Step 2: Calculate \(B^+\)
Start:
\[
B^+ = {B}
\]
Using \(B \rightarrow D\):
\[
B^+ = {B,D}
\]
B alone is also not a superkey.
Step 3: Calculate \((AB)^+\)
Start:
\[
(AB)^+ = {A,B}
\]
Using \(A \rightarrow C\):
\[
(AB)^+ = {A,B,C}
\]
Using \(B \rightarrow D\):
\[
(AB)^+ = {A,B,C,D}
\]
Therefore:
\[
(AB)^+ = R
\]
AB is a superkey.
Step 4: Check Minimality
- A alone is not a superkey.
- B alone is not a superkey.
Therefore, neither attribute can be removed from AB.
Hence:
\[
\boxed{AB \text{ is a Candidate Key}}
\]
9. A Systematic Method for Key Identification
When a relation has several attributes and functional dependencies, use the following approach.
Step 1: Write All Attributes
Clearly identify the complete set of attributes in the relation.
Step 2: Examine the Functional Dependencies
Determine which attributes can derive other attributes.
Step 3: Identify Attributes Not Appearing on the Right Side
Attributes that never appear on the right side of any functional dependency cannot be derived from other attributes using those dependencies.
They must therefore be included in every candidate key.
Step 4: Calculate Attribute Closure
Start with the required attributes and repeatedly apply functional dependencies until no new attributes can be added.
Step 5: Check Whether the Closure Contains All Attributes
If:
\[
X^+ = R
\]
then X is a superkey.
Step 6: Test Minimality
Remove attributes one at a time.
If removing an attribute causes the closure to stop containing all relation attributes, that attribute is necessary.
A minimal superkey is a candidate key.
Step 7: Check Other Possible Combinations
There may be multiple candidate keys. Do not stop after finding the first one.
10. Key Identification: Quick Example
Consider:
\[
R(A,B,C,D,E)
\]
Given:
\[
F = \{
\begin{aligned}
A &\rightarrow B \\
BC &\rightarrow D \\
D &\rightarrow E
\end{aligned}
\}
\]
Find the candidate key.
Notice that C never appears on the right side of any functional dependency. Therefore, C cannot be derived from other attributes and must be included in every candidate key.
Now calculate \((AC)^+\).
Start:
\[
(AC)^+ = {A,C}
\]
Using \(A \rightarrow B\):
\[
(AC)^+ = {A,B,C}
\]
Using \(BC \rightarrow D\):
\[
(AC)^+ = {A,B,C,D}
\]
Using \(D \rightarrow E\):
\[
(AC)^+ = {A,B,C,D,E}
\]
Therefore:
\[
(AC)^+ = R
\]
AC is a superkey.
Check minimality:
- \(A^+ = {A,B}\), not all attributes.
- \(C^+ = {C}\), not all attributes.
Neither A nor C can be removed.
Hence:
\[
\boxed{AC \text{ is the Candidate Key}}
\]
Article in a Nutshell
- A superkey uniquely identifies every tuple in a relation.
- A candidate key is a minimal superkey.
- A relation may have several candidate keys.
- The primary key is the candidate key selected to identify records.
- Remaining candidate keys are alternate keys.
- Composite keys use multiple attributes together.
- Attribute closure helps determine whether an attribute set is a superkey.
- Minimality must be checked to confirm that a superkey is a candidate key.
Identifying keys correctly is essential for database design. In the next article, we will explore why poor database design causes redundancy and anomalies, and how normalization helps solve these problems.
Quick quiz
5 questionsOriginally published at farhanpaal.com — https://farhanpaal.com/articles/candidate-keys-superkeys-and-key-identification/
Keep a copy
Read offline as a printout or PDF.
farhanpaal.com