Primary & Foreign Keys
In a relational database, we don't store everything in one giant table. Instead, we split data into smaller, logical tables (like Users, Courses, and Enrollments).
Keys are the unique identifiers that allow these tables to talk to each other.
1. The Primary Key (PK)
A Primary Key is a column (or group of columns) that uniquely identifies each row in a table. Think of it as the "Digital DNA" of a record.
The 3 Golden Rules of Primary Keys:
- Unique: No two rows can have the same Primary Key.
- Not Null: It can never be empty. Every record must have an ID.
- Immutable: Once assigned, it should never change.
- Real-World Examples: Your Aadhar Card Number, a Roll Number, or a random UUID (Universally Unique Identifier).
2. The Foreign Key (FK)
A Foreign Key is a column in one table that refers to the Primary Key of another table. This is how we create a "Relationship."
How it works:
Imagine you have a Users table and an Orders table.
- The
Userstable has a PK calleduser_id. - The
Orderstable has its own PK, but it also has a column calleduser_id. - This
user_idin theOrderstable is a Foreign Key pointing back to theUserstable.
Visualizing the Connection
Let's look at how CodeHarborHub might link a student to their enrolled courses:
Referential Integrity
When you use Foreign Keys, the database enforces a rule called Referential Integrity.
This means you cannot have an "Orphan" record. For example, you cannot create an Order for a User that doesn't exist in the Users table. The database will throw an error to protect your data!
Summary Checklist
- I know that a Primary Key uniquely identifies a single row.
- I understand that a Foreign Key links two tables together.
- I know that Primary Keys must be unique and cannot be null.
- I understand that keys prevent "bad data" (orphaned records) from entering the system.
Avoid using real data (like an Email or Phone Number) as a Primary Key. Instead, use an Auto-Incrementing Integer (1, 2, 3...) or a UUID. This ensures that even if a user changes their email, their ID remains the same!