1.3.2 Databases
1.3.2(a) Relational Databases
Modern applications need to store and retrieve large amounts of structured data efficiently. Relational databases organise this data into tables with defined relationships, making it easy to query and maintain.
Key Database Terminology
Understanding these terms is essential for working with relational databases.
Entity
• Student - stores information about students
• Course - stores information about courses
• Order - stores information about customer orders
• Product - stores information about items for sale
• Doctor - stores information about medical staff
Attribute
• StudentID
• FirstName
• LastName
• DateOfBirth
• CourseID
Primary Key
• Must be unique for every record
• Cannot be NULL (empty)
• Should not change over time
• Often an auto-generated ID number
• Each table should have exactly one primary key
• StudentID: S001, S002, S003...
• OrderNumber: ORD-2024-0001
• ISBN: 978-3-16-148410-0
Bad Primary Keys:
• Name (not unique - many people share names)
• Email (might change)
• Phone number (might change)
Foreign Key
Doctor Table:
| DoctorID | Name | Speciality |
|---|---|---|
| D001 | Dr Smith | Cardiology |
| D002 | Dr Jones | Neurology |
| PatientID | Name | DoctorID (FK) |
|---|---|---|
| P001 | John Brown | D001 |
| P002 | Jane White | D001 |
| P003 | Bob Green | D002 |
• DoctorID is the PRIMARY KEY in Doctor table (unique identifier)
• DoctorID appears in Patient table as a FOREIGN KEY
• This links each patient to their doctor
• D001 (Dr Smith) has two patients (P001, P002)
• The foreign key MUST match an existing primary key
Secondary Key
In a Patient table, "Surname" might be a secondary key - it's not unique, but you often search by surname, so indexing it makes queries faster.
1.3.2(b) Entity Relationship Diagrams
Entity Relationship Diagrams (ERDs) visually represent the structure of a database, showing entities and the relationships between them.
One-to-One (1:1)
• Each person has exactly one passport
• Each passport belongs to exactly one person
One-to-Many (1:M)
• One doctor can have MANY patients
• Each patient has only ONE doctor (in this system)
Example: Customer and Orders
• One customer can place MANY orders
• Each order belongs to ONE customer
Many-to-Many (M:M)
Many-to-many relationships require a JUNCTION TABLE (also called a linking table or bridge table) to implement in a relational database.
Problem:
• One student takes MANY courses
• One course has MANY students
• This is many-to-many - can't directly link!
Solution - Junction Table:
Student Table:
| StudentID | Name |
|---|---|
| S001 | Alice |
| S002 | Bob |
| CourseID | Title |
|---|---|
| C001 | Maths |
| C002 | Physics |
| StudentID | CourseID | Grade |
|---|---|---|
| S001 | C001 | A |
| S001 | C002 | B |
| S002 | C001 | B |
• Alice (S001) takes Maths AND Physics
• Bob (S002) takes only Maths
• Maths course has Alice AND Bob
• The junction table resolves the M:M relationship
1.3.2(c) Normalisation
When designing a database, poor structure leads to data redundancy (duplicate data), update anomalies, and wasted storage. Normalisation is the process of organising tables to minimise these problems.
First Normal Form (1NF)
• Each cell contains only atomic (single, indivisible) values
• There are no repeating groups or arrays
• Each record is unique (has a primary key)
NOT in 1NF (repeating groups):
| StudentID | Name | Courses |
|---|---|---|
| S001 | Alice | Maths, Physics, Chemistry |
| S002 | Bob | Maths, Biology |
IN 1NF (atomic values):
| StudentID | Name | Course |
|---|---|---|
| S001 | Alice | Maths |
| S001 | Alice | Physics |
| S001 | Alice | Chemistry |
| S002 | Bob | Maths |
| S002 | Bob | Biology |
Second Normal Form (2NF)
• It is already in 1NF
• All non-key attributes are fully dependent on the ENTIRE primary key (no partial dependencies)
IN 1NF but NOT 2NF:
| StudentID | CourseID | StudentName | CourseName | Grade |
|---|---|---|---|---|
| S001 | C001 | Alice | Maths | A |
| S001 | C002 | Alice | Physics | B |
| S002 | C001 | Bob | Maths | B |
Problem: StudentName depends only on StudentID, not CourseID. CourseName depends only on CourseID, not StudentID. These are PARTIAL dependencies.
IN 2NF (split into separate tables):
Student Table:
| StudentID | StudentName |
|---|---|
| S001 | Alice |
| S002 | Bob |
| CourseID | CourseName |
|---|---|
| C001 | Maths |
| C002 | Physics |
| StudentID | CourseID | Grade |
|---|---|---|
| S001 | C001 | A |
| S001 | C002 | B |
| S002 | C001 | B |
Third Normal Form (3NF)
• It is already in 2NF
• No non-key attribute depends on another non-key attribute (no transitive dependencies)
IN 2NF but NOT 3NF:
| StudentID | Name | TutorID | TutorName | TutorOffice |
|---|---|---|---|---|
| S001 | Alice | T01 | Mr Brown | Room 12 |
| S002 | Bob | T01 | Mr Brown | Room 12 |
| S003 | Charlie | T02 | Ms Green | Room 15 |
IN 3NF (remove transitive dependency):
Student Table:
| StudentID | Name | TutorID |
|---|---|---|
| S001 | Alice | T01 |
| S002 | Bob | T01 |
| S003 | Charlie | T02 |
| TutorID | TutorName | TutorOffice |
|---|---|---|
| T01 | Mr Brown | Room 12 |
| T02 | Ms Green | Room 15 |
• Reduces data redundancy (no duplicate data)
• Prevents update anomalies (change once, applies everywhere)
• Saves storage space
• Improves data integrity
• Makes database easier to maintain
1.3.2(d) SQL (Structured Query Language)
SQL is the standard language for interacting with relational databases. It allows you to create, query, update, and delete data.
SELECT Queries
SELECT FirstName, LastName, Email
FROM Student
WHERE Year = 12
ORDER BY LastName ASC;
This query:
• Selects three columns
• From the Student table
• Only where Year equals 12
• Sorted alphabetically by LastName
-- Select ALL columns from Student
SELECT * FROM Student;
-- Select products where name starts with 'A'
SELECT * FROM Product WHERE Name LIKE 'A%';
Wildcards:
• % matches any number of characters
• _ matches exactly one character
• LIKE 'A%' finds anything starting with A
• LIKE '_ones' finds 5-letter words ending in 'ones'
JOIN Queries
Tables:
• Student: StudentID, Name, TutorID
• Tutor: TutorID, TutorName, Department
SELECT Student.Name, Tutor.TutorName, Tutor.Department
FROM Student
JOIN Tutor ON Student.TutorID = Tutor.TutorID
WHERE Tutor.Department = 'Computer Science';
Result: Names of students and their tutors, but only for the Computer Science department
Data Modification Commands
INSERT INTO Student (Name, Year)
VALUES ('Alice', 12);
UPDATE - Modify existing records:
UPDATE Student
SET Year = 13
WHERE StudentID = 'S001';
DELETE - Remove records:
DELETE FROM Student
WHERE Year = 11;
CREATE TABLE - Make a new table:
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Year INT,
Email VARCHAR(100)
);
ALTER TABLE - Change table structure:
ALTER TABLE Student
ADD Email VARCHAR(100);
• SELECT - retrieve data from tables
• INSERT INTO - add new records
• UPDATE - modify existing records
• DELETE FROM - remove records
• CREATE TABLE - create a new table
• ALTER TABLE - modify table structure
• DROP TABLE - delete a table entirely
• JOIN - combine data from multiple tables
• WHERE - filter results by condition
• ORDER BY - sort results (ASC/DESC)
• LIKE - pattern matching with wildcards
1.3.2(e) Referential Integrity
• Cannot insert a record with a foreign key that doesn't exist in the parent table
• Cannot delete a parent record if child records reference it
• Cannot update a primary key if foreign keys reference it
Enforcement:
Modern DBMSs enforce these rules automatically when foreign key constraints are defined.
Tables:
• Department: DeptID (PK), DeptName
• Employee: EmpID (PK), Name, DeptID (FK)
Valid Operation:
Insert Employee with DeptID = 'D001' only if 'D001' exists in Department table
Invalid Operation:
Insert Employee with DeptID = 'D999' if 'D999' doesn't exist - would violate referential integrity
Cascading Actions:
• ON DELETE CASCADE: Delete child records when parent deleted
• ON UPDATE CASCADE: Update foreign keys when primary key changes
1.3.2(f) Query by Example (QBE)
• Visual interface - no need to learn SQL syntax
• Fill in values in a grid/table format
• Criteria entered in same row = AND
• Criteria entered in different rows = OR
• Check boxes select which fields to display
Query: Find all Year 12 students studying Computer Science
| Field | StudentID | Name | Year | Course |
|---|---|---|---|---|
| Show | ☑ | ☑ | ☐ | ☐ |
| Criteria | 12 | CompSci |
• Row criteria: Year = 12 AND Course = 'CompSci'
• Show ticked for StudentID and Name only
• Result shows StudentID and Name of matching records
For OR conditions, use multiple rows:
| Field | Year | Course |
|---|---|---|
| Criteria Row 1 | 12 | |
| Criteria Row 2 | Maths |
| Feature | SQL | QBE |
|---|---|---|
| User interface | Text commands | Visual grid |
| Learning curve | Steeper | Easier for beginners |
| Flexibility | Very flexible | Limited but intuitive |
| Precision | Exact syntax required | Visual selection |
| Use case | Programmers, power users | End users, simple queries |