Relational Database Setup (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Relational database

  • A relational database stores data across multiple linked tables rather than one large flat file

  • Each table stores data about one type of entity (for example, Students or Tutors)

  • Relationships between the tables are created using keys:

    • A primary key (PK) uniquely identifies each record in a table

    • A foreign key (FK) is a field that links one table to another

  • Using keys to link data:

    • Removes data redundancy (duplicate data)

    • Makes the database more efficient to store and maintain

    • Prevents inconsistencies when data changes

Example

  • A school originally stores all data in one flat-file table, including each student’s tutor details

  • This causes duplication because the tutor’s name and form room appear many times

  • To solve this, the data is split into two tables:

Student

StudentID (PK)

Forename

Surname

TutorID (FK)

Tutor

TutorID (PK)

TutorName

FormRoom

  • The TutorID field links the two tables

  • Now, the tutor’s name and form room are stored only once in the Tutor table

  • If a tutor changes their form room, this update happens in one place and applies everywhere the data is linked

  • This avoids inconsistency

Referential integrity

What is referential integrity?

  • Referential integrity keeps the relationships between tables valid and consistent

  • It ensures that every foreign key value in one table matches an existing primary key value in the related table

  • If a matching primary key does not exist, the relationship is broken, and the database is said to have lost referential integrity

Example

  • Using the Student and Tutor tables:

    • Each student record includes a TutorID (FK)

    • That TutorID must exist in the Tutor table as a primary key (PK)

    • If a tutor is deleted, the database will stop the change until all linked students are updated

Why it matters

  • Maintaining referential integrity:

    • Prevents orphaned records (foreign keys with no match)

    • Ensures data accuracy and consistency

    • Makes the database easier to maintain

How it is enforced

  • Database management systems (DBMS) can automatically enforce referential integrity by:

    • Preventing updates or deletions that would break table links

Worked Example

A veterinary practice uses a relational database to store details of pet owners and their pets. The database contains two tables: Owner and Pet.

Sample Data:

Owner

ownerID

forename

surname

telephone

OW01

Jack

Jarvis

0141 555 0199

OW02

Victor

McDade

0141 555 0122

OW03

Isa

Drennan

0141 555 0133

Pet

petID

petName

type

age

ownerID

P104

Winston

Dog

5

OW03

P105

Boabby

Cat

2

OW01

P106

Navid

Hamster

1

OW03

(i) State the purpose of the foreign key ownerID in the Pet table

[1]

(ii) A receptionist attempts to add a new pet to the database. Explain why it is better to have referential integrity enforced before adding this new record

[2]

Answers

(i)

  • Create a relationship between the Owner and Pet tables [1 mark]

OR

  • Link the Pet table to the Owner table [1 mark]

(ii)

  • Ensure the pet is linked to an existing owner [1 mark]

  • So you cannot add a pet for an owner ID that does not exist in the Owner table [1 mark]

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.