Primary Keys and Foreign Keys Explained
Master the art of connecting data — a critical CSEC IT skill for building powerful databases!
The Power of "Unique": The Primary Key
What is a Primary Key?
A Primary Key is a special field that uniquely identifies every single record in a table. Think of it as each record's personal ID badge — no two records in the same table can have the same Primary Key value!
The Golden Rules of Primary Keys
- Rule 1: No Nulls Allowed — Every record MUST have a Primary Key value. You cannot leave it empty!
- Rule 2: Must Be Unique — No duplicates allowed. Each value can appear only once in the table.
- Rule 3: One Per Table — A table can have only ONE Primary Key (though it can be made of multiple fields).
CSEC Example: Why Names Don't Work
Imagine using "Student_Name" as an identifier. What happens when you have two students named "Alex Smith"? The database can't tell them apart! That's why we use Student_ID instead — each student gets their own unique number that nobody else has.
Try to "check in" to a school event! Enter an ID to see if it's accepted or rejected.
The Bridge: The Foreign Key
What is a Foreign Key?
A Foreign Key is a field in ONE table that refers to (points to) the Primary Key of ANOTHER table. It creates a "bridge" or link between different tables without duplicating data!
The Purpose of Foreign Keys
Imagine a school with hundreds of students, each taking multiple courses. Instead of copying all student information into every course record, we simply use the Student_ID as a Foreign Key. This:
- Saves storage space
- Keeps data consistent (no conflicting addresses)
- Creates relationships between different subjects
Visualizing the Link
The Parent table contains the master information, while the Child table references it:
- Parent Table: Department_Table (PK: Dept_ID)
- Child Table: Teacher_Table (FK: Dept_ID)
Click on a Department ID in the Parent table to see how it connects to the Foreign Keys in the Child table!
Types of Relationships
One-to-One (1:1)
One record in Table A relates to exactly ONE record in Table B. Example: One person has one passport. Each passport belongs to one person.
One-to-Many (1:M) — The Most Common!
One record in Table A relates to MANY records in Table B. Example: One teacher has many students. But each student has only one homeroom teacher.
Many-to-Many (M:N)
Many records in Table A relate to MANY records in Table B. Example: Many students take many different subjects. This relationship usually requires a "junction table" in between.
Drag each scenario to the correct relationship type!
One-to-One
One-to-Many
Many-to-Many
Referential Integrity: Keeping the Bridge Strong
What is Referential Integrity?
Referential Integrity is a set of rules that ensures the "bridge" between tables remains valid and consistent. It prevents "orphan" records — data in one table that points to something that doesn't exist in another table.
The "Orphan" Problem
Imagine deleting a student from the Students table, but their enrollment records still exist in a Grades table. Those Grade records are now "orphans" — they point to a Student_ID that doesn't exist! Referential integrity prevents this by:
- Preventing deletion of records that are referenced by other tables
- Ensuring Foreign Key values always point to valid Primary Keys
- Maintaining consistency across related tables
Try to delete a student who has enrolled courses. The Guard will warn you about breaking referential integrity!
⚠️ INTEGRITY ALERT!
Cannot delete! This record is referenced by other tables.
Compound Keys (Advanced CSEC Concept)
What is a Compound Key?
A Compound Key (also called a Composite Key) is when TWO or MORE fields are combined together to create a unique Primary Key. Neither field alone is unique, but together they create a unique combination!
CSEC Example: Class Attendance
Imagine tracking student attendance. If we use just Student_ID, the same student would appear multiple times (one for each day). If we use just Date, many students would have the same date. But Student_ID + Date together creates a unique record for each student's attendance on each day!
Select the TWO fields that, when combined, create a unique Primary Key for this scenario!
Scenario: Library Book Borrow Records
The library needs to track which member borrowed which book on which date. Each borrow event should have a unique record.
CSEC SBA Prep: Designing Your Relationships
How to Draw an ER Diagram
For your SBA, you'll often need to show your database design visually. Follow these steps:
- Draw each table as a rectangle
- List all fields inside, marking the Primary Key (PK) and Foreign Keys (FK)
- Draw lines between tables to show relationships
- Use "crow's foot" notation (1, M, or N) to show relationship types
Example ER Diagram: Student Course Enrollment
Pre-Submission Checklist
Before submitting your SBA, check that your relationships are correct:
Knowledge Check: The "Key" Master Quiz
Test your understanding of Primary Keys and Foreign Keys with these scenario-based questions!
Answer the questions. You have 20 seconds per question!
Quiz Complete!
Primary Key
A unique identifier for each record in a table. Cannot be null or duplicated.
Foreign Key
A field that references the Primary Key of another table, creating a relationship.
Referential Integrity
Rules ensuring that relationships between tables remain valid and consistent.
Compound Key
A Primary Key made from two or more fields combined to ensure uniqueness.
