Primary Keys
What you'll learn: How primary keys uniquely identify each row in a database table to ensure no record is confused with another.
What is a Primary Key?
A primary key is a special column (or combination of columns) in a database table that uniquely identifies every single row. Think of it like a student ID number in a school: even if two students have the same name, their ID numbers are always different, letting you distinguish between them.
In the relational model, primary keys solve a fundamental problem: how do we refer to a specific record without ambiguity? Without primary keys, you'd have no guaranteed way to point to exactly one row, especially when data values might be duplicated.
Core Properties of Primary Keys
A valid primary key must satisfy two critical rules:
- Uniqueness: No two rows can have the same primary key value
- Non-null: The primary key cannot be empty (NULL) — every row must have a value
These constraints are enforced by the database management system, not just by convention.
Example
Imagine a Students table:
StudentID | Name | Email
----------|---------|------------------
101 | Alice | alice@school.edu
102 | Bob | bob@school.edu
103 | Alice | alice2@school.edu
Here, StudentID serves as the primary key. Notice two students named "Alice" exist, but their unique StudentID values (101 and 103) let us distinguish them perfectly.
Why Primary Keys Matter
Primary keys form the foundation for relationships between tables and enable efficient data retrieval. They guarantee that when you reference a specific record, you're talking about exactly that record and no other.
Key Takeaway: A primary key is a unique, non-null identifier for each row in a table, ensuring every record can be distinctly identified and referenced.