🔍 Candidate Key
🟢 Beginner
📖 Definition
A Candidate Key is a column or a set of minimal columns that uniquely identifies a row in a database table without any redundant attributes. A table can have multiple Candidate Keys. From these candidate keys, database designers select ONE to serve as the Primary Key, while all remaining candidate keys become Alternate Keys.
🌍 Multilingual Explanations
English
A Candidate Key is a minimal Super Key that uniquely identifies a table row. Every Candidate Key qualifies to become a Primary Key.
Hindi
Candidate Key ek aisa column ya attribute group hota hai jo har row ko uniquely identify kar sakta hai. Isme se ek key ko Primary Key banaya jaata hai.
Marathi
Candidate Key mhanje Primary Key bananyasathi eligible aslele unique columns. Yantil eka key la Primary Key mhanun nivadle jaate.
Hinglish
Candidate Keys are "candidates" competing for the Primary Key role — for example, both `employee_id` and `passport_number` are Candidate Keys.
⚖️ Candidate Key vs. Primary Key Comparison
| Property | Candidate Key | Primary Key |
|---|---|---|
| Quantity | A table can have multiple Candidate Keys. | A table can have only ONE Primary Key. |
| NULL Values | Can theoretically contain NULL if defined without constraints (though best practice uses NOT NULL). |
Strictly CANNOT contain NULL. |
| Selection | Selected during database ER modeling. | Chosen specifically from the set of Candidate Keys. |
⚡ SQL Syntax
-- Defining Candidate Keys using PRIMARY KEY and UNIQUE NOT NULL constraints
CREATE TABLE registered_users (
user_id INT PRIMARY KEY, -- Candidate Key 1 (Selected as Primary Key)
email VARCHAR(100) NOT NULL UNIQUE, -- Candidate Key 2 (Enforced with UNIQUE + NOT NULL)
national_id VARCHAR(20) NOT NULL UNIQUE -- Candidate Key 3 (Enforced with UNIQUE + NOT NULL)
);
💡 Production Example
-- In an Employees table, both employee_id and ssn (Social Security Number) are Candidate Keys
CREATE TABLE company_employees (
emp_id INT PRIMARY KEY,
ssn CHAR(9) NOT NULL UNIQUE,
full_name VARCHAR(100) NOT NULL
);
-- Both queries return exactly ONE unique record:
SELECT * FROM company_employees WHERE emp_id = 501;
SELECT * FROM company_employees WHERE ssn = '123456789';
⚠️ Common Pitfalls & Mistakes
- Confusing Super Key with Candidate Key: A Super Key can contain extra non-essential columns, whereas a Candidate Key must be minimal (removing any column breaks uniqueness).
- Allowing NULL in Candidate Keys: Always add
NOT NULLtoUNIQUEcandidate key columns to prevent unidentifiable records.
🎯 Tiered Practice Exercises
- Beginner: Identify all candidate keys in a table containing
student_id,aadhaar_no,email, andclass_name. - Intermediate: Write a
CREATE TABLEquery enforcing two candidate keys usingPRIMARY KEYandUNIQUE NOT NULL. - Advanced: Explain why a composite key of
(user_id, email)is a Super Key but NOT a Candidate Key ifuser_idalone is already unique.