Primary Keys and Foreign Keys Explained

Master the art of connecting data — a critical CSEC IT skill for building powerful databases!

1

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.

The ID Scanner

Try to "check in" to a school event! Enter an ID to see if it's accepted or rejected.

🔍
Enter an ID to check in...
2

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)
The Relationship Mapper

Click on a Department ID in the Parent table to see how it connects to the Foreign Keys in the Child table!

DEPARTMENTS (Parent Table)
D001
Mathematics
D002
Science
D003
English
TEACHERS (Child Table - FK: Dept_ID)
T001
Mrs. Williams
D002
T002
Mr. Brown
D001
T003
Ms. Johnson
D003
T004
Dr. Davis
D002
Click a Primary Key to see its connections!
3

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.

Relationship Sort

Drag each scenario to the correct relationship type!

Customer to Orders
Person to Passport
Students to Subjects
Teacher to Students
Employee to Employee_ID
Doctors to Patients
1:1

One-to-One

1:M

One-to-Many

M:N

Many-to-Many

4

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
The Integrity Guard

Try to delete a student who has enrolled courses. The Guard will warn you about breaking referential integrity!

STUDENTS (Parent)
S001
Jordan Williams
S002
Ashley Brown
S003
Marcus Johnson
ENROLLMENTS (Child - FK: Student_ID)
E001
S001
Math 101
E002
S002
Science 101
E003
S001
English 101
E004
S003
History 101

⚠️ INTEGRITY ALERT!

Cannot delete! This record is referenced by other tables.

5

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!

The Key Combiner

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.

Member_ID
Book_ID
Borrow_Date
Member_Name
Select two fields to create a Compound Key...
6

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:

  1. Draw each table as a rectangle
  2. List all fields inside, marking the Primary Key (PK) and Foreign Keys (FK)
  3. Draw lines between tables to show relationships
  4. Use "crow's foot" notation (1, M, or N) to show relationship types

Example ER Diagram: Student Course Enrollment

STUDENTS
Student_ID (PK)
First_Name
Last_Name
Email
1:M
ENROLLMENTS
Enrollment_ID (PK)
Student_ID (FK)
Course_ID (FK)
Grade
M:1
COURSES
Course_ID (PK)
Course_Name
Credits

Pre-Submission Checklist

Before submitting your SBA, check that your relationships are correct:

Every table has a defined Primary Key
Each Foreign Key matches the data type of its corresponding Primary Key
No "circular" relationships (Table A → Table B → Table A)
Relationship types (1:1, 1:M, M:N) are correctly identified
Many-to-Many relationships have junction tables
7

Knowledge Check: The "Key" Master Quiz

Test your understanding of Primary Keys and Foreign Keys with these scenario-based questions!

CSEC Key Master Quiz

Answer the questions. You have 20 seconds per question!

Loading question...
Score: 0/5

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.

Scroll to Top