What is a primary key?
When we work with databases, one of the first ideas we must understand is how to uniquely identify each record. Just like every student in a class has a unique roll number, every row in a database table also needs an identity.
This unique identity is what we call a primary key.
A primary key is a column—or sometimes a set of columns—in a table that uniquely identifies every single row. No two rows in the table can share the same primary key value, and it cannot be left empty.
Key points to remember:
- It must be unique
- It cannot be null
- It helps the database quickly find, update, or delete a specific record
- It keeps the data organised and prevents duplicates
Simple Example
Imagine a table that stores student information:
| StudentID | Name | Age |
|---|---|---|
| 101 | Aditi | 20 |
| 102 | Rohan | 21 |
| 103 | Meera | 19 |
Here, StudentID serves as the primary key because it uniquely identifies each student. Even if two students share the same name or age, their IDs will always be different.
Why do we need a primary key?
Without a primary key, the database would not know which record is which. It would struggle to maintain order, avoid duplicates, or connect tables together using relationships.
So, in simple terms:
The primary key is the backbone of table structure and data integrity.
A primary key is a column or set of columns in a table that uniquely identifies each row in the table. It is used to enforce data integrity and ensure that each row in the table is unique.