Cambridge IGCSE ICT 0417 – Databases: Primary and Foreign Keys
Topic 18 – Databases
Objective
Be able to create and edit primary and foreign keys.
1. What is a Primary Key?
A primary key is a field (or combination of fields) that uniquely identifies each record in a table.
Must contain a unique value for every record.
Cannot contain NULL (empty) values.
Only one primary key per table, but it may be composed of multiple fields (composite key).
2. Creating a Primary Key
Steps to create a primary key in most database software (e.g., Microsoft Access, MySQL Workbench):
Open the table in Design/View mode.
Select the field that will serve as the primary key.
Set the field property Primary Key (often a button or checkbox).
Save the table structure.
3. Editing a Primary Key
When a primary key needs to be changed:
Open the table in Design mode.
Remove the existing primary key designation (clear the Primary Key property).
Make any required changes to the field(s) (e.g., change data type, rename).
Select the new field(s) and set them as the primary key.
Save the table. The database will enforce uniqueness on the new key.
4. What is a Foreign Key?
A foreign key is a field (or combination of fields) in one table that refers to the primary key of another table, establishing a relationship between the two tables.
Ensures referential integrity – records in the child table must correspond to existing records in the parent table.
Can contain duplicate values (many‑to‑one relationship).
May allow NULL if the relationship is optional.
5. Creating a Foreign Key
Typical steps (illustrated for a relational database management system):
Identify the parent table and its primary key.
In the child table, add a field with the same data type as the parent primary key.
Open the table design view and select the field.
Define a Relationship (often via a Relationships window or ALTER TABLE statement):