What is a Candidate Key in SQL?
A Candidate Key in SQL is a column or combination of columns that can uniquely identify each row in a table.
In simple words:
A candidate key is a possible column that can become a primary key.
Why Candidate Key is Important
In a database table:
- Multiple columns may uniquely identify records
Among those unique columns:
- One is selected as Primary Key
- Remaining are Candidate Keys
Real-Time Example
Suppose a learning platform stores student details.
Students Table
| Student ID | Phone Number | Name | |
|---|---|---|---|
| 101 | naresh@gmail.com | 9876543210 | Naresh |
| 102 | rahul@gmail.com | 9876500000 | Rahul |
Understanding Candidate Keys
In this table:
- Student ID is unique
- Email is unique
- Phone Number is unique
All these columns can uniquely identify rows.
Therefore:
- Student ID
- Phone Number
are:
Candidate Keys
What Happens Next?
Among candidate keys:
- One key is selected as Primary Key
Example:
Student ID -> PRIMARY KEY
Remaining Candidate Keys
Email Phone Number
usually become:
UNIQUE KEYS
Candidate Key Architecture
Table
|
--------------------------------------
| | | |
v v v v
StudentID Email Phone Name
| | |
| | |
------------ Candidate Keys
|
v
One Selected as
Primary Key
Characteristics of Candidate Key
- Must uniquely identify rows
- Cannot contain duplicate values
- Should not contain NULL values
- Can be single column or multiple columns
Candidate Key vs Primary Key
| Feature | Candidate Key | Primary Key |
|---|---|---|
| Purpose | Possible unique identifier | Selected unique identifier |
| Count | Multiple allowed | Only one allowed |
| NULL Allowed | No | No |
| Uniqueness | Required | Required |
Simple Understanding
All Primary Keys are Candidate Keys.
But:
Not all Candidate Keys become Primary Keys.
Example
Candidate Keys:
Student ID Email Phone Number
Chosen Primary Key
Student ID
Other Candidate Keys
Email Phone Number
become alternate unique identifiers.
How Candidate Keys are Identified
A candidate key must:
- Uniquely identify rows
- Contain minimal columns
What is Minimal Column?
Minimal means:
No unnecessary columns included.
Bad Example
(Student ID, Name)
Not minimal because:
Student ID
alone is sufficient.
Candidate Key Example in SQL
CREATE TABLE students (
student_id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE,
phone VARCHAR(15) UNIQUE,
name VARCHAR(100)
);
Explanation
| Column | Type |
|---|---|
| student_id | Primary Key |
| Candidate Key | |
| phone | Candidate Key |
What is Alternate Key?
Candidate keys that are not selected as primary key are called:
Alternate Keys
Example
If:
student_id
is primary key:
then:
email phone
become:
Alternate Keys
Candidate Key vs Unique Key
| Feature | Candidate Key | Unique Key |
|---|---|---|
| Concept | Logical database design | Database constraint |
| Uniqueness | Required | Required |
| NULL Allowed | No | Usually one NULL |
| Purpose | Possible primary key | Prevent duplicates |
Composite Candidate Key
A candidate key can use multiple columns together.
Example
(student_id, course_id)
can uniquely identify enrollment records.
Composite Candidate Key Example
CREATE TABLE enrollments (
student_id INT,
course_id INT,
grade VARCHAR(10),
UNIQUE(student_id, course_id)
);
Why Composite Candidate Key is Used
Prevents:
- Same student enrolling in same course multiple times
Candidate Key Query Flow
Insert Data
|
v
Candidate Key Validation
|
v
Duplicate Check
|
v
Data Stored
Real-Time Banking Example
Banking systems may use:
- Account Number
- PAN Number
as candidate keys.
Real-Time E-Commerce Example
E-commerce systems may use:
- Customer ID
- Phone Number
as candidate keys.
Real-Time Hospital Example
Hospital systems may use:
- Patient ID
- Insurance Number
as candidate keys.
Advantages of Candidate Key
- Ensures uniqueness
- Improves database design
- Supports normalization
- Prevents duplicate records
Challenges of Candidate Key
- Choosing wrong candidate key
- Large composite keys affect performance
- Frequent updates create issues
Best Practices
- Choose stable columns
- Avoid frequently changing values
- Prefer numeric primary keys
- Use business identifiers as alternate keys
Related Learning Topics
- What is a Primary Key in SQL?
- What is a Foreign Key in SQL?
- What is a Unique Key in SQL?
- Database Normalization Principles
- MySQL Constraints Primary and Foreign Keys
Candidate Key in Normalization
Candidate keys play an important role in:
- Normalization
- Relationship design
- Database optimization
Candidate Key in Microservices
In microservices architecture:
- Each service database may define candidate keys independently
Example
User Service
|
v
email
Payment Service
|
v
transaction_id
Professional Interview Answer
A Candidate Key in SQL is a column or combination of columns that can uniquely identify each row in a table. A table may contain multiple candidate keys, but only one candidate key is selected as the primary key. Candidate keys must contain unique and non-null values and should use minimal columns required for uniqueness. Candidate keys are important in database design, normalization, and maintaining data integrity.
Why Interviewers Like This Answer
- Clearly explains uniqueness concept
- Compares candidate key with primary key
- Includes real-world examples
- Shows database design understanding
- Covers normalization concepts
Frequently Asked Questions
What is a candidate key?
A candidate key is a column or set of columns that can uniquely identify rows in a table.
Can a table have multiple candidate keys?
Yes, multiple candidate keys are allowed.
What is difference between candidate key and primary key?
Candidate key is a possible primary key, while primary key is the selected candidate key.
Can candidate key contain NULL values?
No, candidate keys should not contain NULL values.
What is alternate key?
Candidate keys not selected as primary key are called alternate keys.