Know and understand the characteristics of primary keys (PK) and foreign keys (FK), how to choose appropriate data‑types, and how to use them to maintain data integrity throughout the design, form‑building and data‑manipulation stages of the database life‑cycle.
During the Design stage (Section 7 of the syllabus) the analyst creates a logical data model. Selecting suitable primary and foreign keys at this point ensures:
A primary key (PK) is one or more fields whose combined values uniquely identify each record in a table.
INTEGER or BIGINT) for surrogate keys; short CHAR/VARCHAR for natural keys that are guaranteed unique.| Aspect | Surrogate (system‑generated) | Natural (business‑derived) |
|---|---|---|
| Typical data‑type | INTEGER / BIGINT (auto‑increment) | VARCHAR, CHAR, DATE, etc. |
| Stability | Always stable | May change (e.g. a product code) |
| Privacy | Does not expose personal data | May contain personal or sensitive information |
| Performance | Fast indexing & joins | Slower if long or composite |
| When to use | Large tables, no guaranteed unique business attribute | When a single business attribute is already unique (e.g. ISBN) |
CREATE TABLE OrderDetails (OrderID INT NOT NULL,
ProductID INT NOT NULL,
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);
The combination (OrderID, ProductID) ensures that the same product cannot appear twice on the same order.
INTEGER) unless a natural attribute is guaranteed unique.A foreign key (FK) is a field (or group of fields) in a child (referencing) table that points to the primary key of a parent (referenced) table, thereby establishing a relationship between the two tables.
ON UPDATE CASCADE – Change in the parent PK is automatically reflected in the child FK.ON DELETE CASCADE – Deleting a parent row also deletes all child rows that reference it.ON DELETE SET NULL – Deleting a parent row sets the child FK to NULL (used for optional links).ON DELETE RESTRICT – Prevents deletion of a parent row while child rows still reference it.CREATE TABLE Students (StudentID INT PRIMARY KEY AUTO_INCREMENT,
FullName VARCHAR(50) NOT NULL,
DateOfBirth DATE
);
CREATE TABLE Courses (
CourseCode CHAR(6) PRIMARY KEY,
CourseName VARCHAR(100) NOT NULL
);
CREATE TABLE Enrollments (
StudentID INT,
CourseCode CHAR(6),
EnrolDate DATE,
PRIMARY KEY (StudentID, CourseCode), -- composite PK
FOREIGN KEY (StudentID) REFERENCES Students(StudentID)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (CourseCode) REFERENCES Courses(CourseCode)
ON UPDATE CASCADE ON DELETE SET NULL
);
| Aspect | Primary Key | Foreign Key |
|---|---|---|
| Purpose | Uniquely identifies each record within its own table. | Creates a logical link to a record in another (parent) table. |
| Uniqueness | Must be unique. | Uniqueness not required; duplicates allowed. |
| Null values | Never NULL. | May be NULL if the relationship is optional. |
| Location | Defined in the table it identifies. | Defined in the child (referencing) table. |
| Number per table | Exactly one (composite counts as one). | Zero, one or many. |
| Integrity enforced | Entity‑integrity. | Referential‑integrity. |
| Typical actions | None (value is static). | ON UPDATE/DELETE actions (CASCADE, SET NULL, RESTRICT). |
INT for IDs, CHAR/VARCHAR for codes, DATE for dates).ON UPDATE/ON DELETE actions.When creating a data‑entry form that reflects the logical model, follow these guidelines:
INT or BIGINT → numeric textbox (right‑aligned).CHAR/VARCHAR → text box (max length set to field size).DATE → date picker.Students must be able to retrieve, sort, search and calculate data using SQL SELECT statements.
SELECT StudentID, FullNameFROM Students
WHERE DateOfBirth > '2005-01-01';
SELECT CourseCode, CourseNameFROM Courses
ORDER BY CourseName ASC;
SELECT s.FullName, c.CourseName, e.EnrolDateFROM Enrollments e
JOIN Students s ON e.StudentID = s.StudentID
JOIN Courses c ON e.CourseCode = c.CourseCode
WHERE e.EnrolDate BETWEEN '2023-01-01' AND '2023-12-31'
ORDER BY s.FullName;
SELECT c.CourseCode, COUNT(e.StudentID) AS NumStudentsFROM Enrollments e
JOIN Courses c ON e.CourseCode = c.CourseCode
GROUP BY c.CourseCode
HAVING COUNT(e.StudentID) > 10;
StudentID – PK, INT AUTO_INCREMENT (surrogate)FullName – VARCHAR(50)DateOfBirth – DATECourseCode – PK, CHAR(6) (natural)CourseName – VARCHAR(100)StudentID – FK → Students.StudentIDCourseCode – FK → Courses.CourseCodeEnrolDate – DATEStudentID, CourseCode)Key benefits of this design:
ON DELETE CASCADE indiscriminately – can cause unintended mass deletions.Primary keys enforce entity‑integrity by uniquely identifying each record; foreign keys enforce referential‑integrity by linking related records across tables. Selecting appropriate data‑types, deciding between surrogate and natural keys, defining constraints correctly, and reflecting the logical model in both forms and queries are essential skills for the ICT 0417 IGCSE/A‑Level syllabus.
Students (PK StudentID) → Enrollments (FK StudentID) ← Courses (PK CourseCode) – a many‑to‑many relationship via the junction table.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.