Primary keys and foreign keys are fundamental concepts in IB Computer Science databases, yet many students confuse their roles or describe them too vaguely. In IB exams, students are expected to explain what each key is, why it is needed, and how keys are used to link tables together.
Clear understanding of keys is essential for topics such as relational databases, normalisation, and SQL queries.
What Is a Primary Key?
A primary key is a field (or combination of fields) that uniquely identifies each record in a table.
Key characteristics of a primary key:
- It must be unique
- It must never be empty (null)
- It identifies one and only one record
For example:
- A student ID in a student table
- An order number in an orders table
Even if two records share similar data, their primary key values must always be different.
Why Primary Keys Are Important
Primary keys are essential because they:
- Prevent duplicate records
- Allow records to be identified accurately
- Enable efficient searching and indexing
- Support relationships between tables
Without a primary key, a database cannot reliably manage or reference individual records.
In IB terms, the primary key ensures data integrity.
What Is a Foreign Key?
A foreign key is a field in one table that refers to the primary key of another table.
Its purpose is to:
- Create a link between two tables
- Represent relationships between data entities
For example:
- A student ID stored in an enrolments table
- A customer ID stored in an orders table
The foreign key value must match an existing primary key value in the related table.
How Primary and Foreign Keys Work Together
Primary and foreign keys work together to:
- Connect related data
- Avoid data duplication
- Maintain consistency across tables
Instead of storing the same information repeatedly, databases store it once and use keys to reference it.
For example:
- A student’s name is stored once in the student table
- The student ID is referenced in other tables
This structure makes databases more efficient and reliable.
Keys and Relational Databases
IB Computer Science focuses mainly on relational databases, where:
- Data is stored across multiple tables
- Tables are linked using keys
Primary and foreign keys define these relationships and allow:
- Complex queries
- Accurate joins
- Consistent updates
Understanding keys is essential for understanding how relational databases function.
Common Student Mistakes
Students often:
- Say a primary key is “any important field”
- Forget that primary keys must be unique
- Confuse primary and foreign keys
- Forget that foreign keys reference primary keys
Precise definitions are required for full marks.
How Keys Appear in IB Exams
IB exam questions may ask students to:
- Define primary and foreign keys
- Identify suitable keys for a table
- Explain how tables are linked
- Justify database design choices
Clear explanations and correct terminology score highest.
Why Keys Matter Beyond Exams
Primary and foreign keys are not just theoretical concepts. They are used in:
- School databases
- Banking systems
- Online shopping platforms
- Social media systems
Any system that manages large amounts of structured data relies on keys.
Final Thoughts
Primary keys uniquely identify records, while foreign keys link tables together by referencing those primary keys. Together, they form the backbone of relational databases.
Understanding how primary and foreign keys work allows IB Computer Science students to explain database structure, relationships, and efficiency clearly and confidently — exactly what examiners expect.
