Primary, Candidate & Alternate Keys
A table full of students is not much use if you cannot point at one of them and be sure which one you mean. Two students can share a name, a class, a city and even a birthday. Something has to tell them apart, and that something is a key.
1Why a key is needed
Suppose the office keeps only these three columns, and two students happen to be called Ravi:
| name | grade | city |
|---|---|---|
| Ravi | 12 | Gangtok |
| Ravi | 12 | Gangtok |
| Diana | 11 | Kolkata |
Now “update Ravi's marks” is an impossible instruction. There is no sentence you can write that means the first Ravi and not the second. The rows are indistinguishable, and the database has no way to help you.
The fix is to add a column that is guaranteed never to repeat — an admission number:
| id | name | grade | city |
|---|---|---|---|
| 2035 | Ravi | 12 | Gangtok |
| 2601 | Ravi | 12 | Gangtok |
| 2199 | Diana | 11 | Kolkata |
2Primary key
A primary key is a column that uniquely identifies each record in a table.
Three rules follow from the word “uniquely”, and all three are examinable:
The same value cannot appear twice. If 2035 is Ravi's, it can never be anyone else's.
A table has exactly one primary key — though that key may be built from more than one column.
Every row must have a value. A row with no id could not be identified, which defeats the point.
3Candidate keys
Often more than one column could do the job. Take a table of the school's employees:
| id | name | designation | PAN | city | salary | Aadhaar |
|---|---|---|---|---|---|---|
| 1 | Sunita Rai | PGT | ABCPR1234K | Singtam | 62000 | 4111…3333 |
| 2 | Mohan Das | PGT | BXYPD5678L | Gangtok | 58000 | 4222…4444 |
| 3 | Tashi Bhutia | TGT | CDEPB9012M | Gangtok | 47000 | 4333…5555 |
id, PAN and Aadhaar are each unique to one person. Any one of them could have been made the primary key. Together they are the candidate keys.
The candidate keys are the set of all columns that can become the primary key.
{ id, PAN, Aadhaar }
Note that name, city and salary are not candidates: two employees can easily share any of them.
4Alternate keys
You pick one candidate to be the primary key. The rest do not stop being unique — they just did not get the job.
The candidate keys that were not chosen as the primary key are the alternate keys.
5Composite key
Sometimes no single column is unique. Here nobody has an admission number, and names repeat:
| name | father | mother | dob | grade |
|---|---|---|---|---|
| Ravi | Suresh | Kamla | 2008-08-13 | 12 |
| Ravi | Mahesh | Sita | 2008-11-02 | 12 |
| Diana | Peter | Mary | 2009-05-16 | 11 |
name repeats. But name together with father and mother does not. A group of columns acting jointly as the primary key is a composite key (or composite primary key).
6All four keys together
| Key | What it is | In the employee table |
|---|---|---|
| Candidate key | Every column that could uniquely identify a row | { id, PAN, Aadhaar } |
| Primary key | The candidate actually chosen to identify rows | id |
| Alternate key | The candidates that were not chosen | PAN, Aadhaar |
| Composite key | Several columns acting together as the primary key, used when no single column is unique | not needed here — id is enough on its own |
How many primary keys can a table have?
A table has candidate keys { rollno, aadhaar, email } and rollno is made the primary key. What are aadhaar and email?
Which of these can a primary key column NOT contain?
In a table of match results, neither team_name nor match_date is unique on its own, but together they are. Together they form…