A levels Computer Science · Networks and databases
Relational databases and SQL
1Relational databases
A relational database stores data in tables (relations). Each row is a record and each column is a field (attribute).
- A primary key uniquely identifies each record, such as StudentID.
- A foreign key is a field in one table that is the primary key of another, linking the tables.
- A composite key is a primary key made of two or more fields together.
Storing data in linked tables avoids redundancy (the same data repeated), which saves space and prevents inconsistency (copies that disagree).
The notation Student(StudentID, Name, Year) shows a table, with the primary key underlined or in bold.
Check yourself
What is a foreign key?
Show the answer
A field that is the primary key of another table, used to link the tables
Link between tables.
2Entities and relationships
An entity is a thing we store data about (Student, Course). Relationships between entities can be:
- one-to-one: each person has one passport;
- one-to-many: one teacher teaches many classes;
- many-to-many: students take many courses, and courses have many students.
Resolving a many-to-many relationship. Enrolment links Student and Course, giving two one-to-many relationships.
A many-to-many relationship cannot be stored directly. It is resolved with a link table (here, Enrolment), giving two one-to-many relationships. Enrolment has a composite key (StudentID, CourseID).
Check yourself
How is a many-to-many relationship stored in a relational database?
Show the answer
With a link table that creates two one-to-many relationships
Link entity.
3Normalisation
Normalisation organises tables to remove redundancy.
| Normal form | Rule |
|---|---|
| first (1NF) | no repeating groups; every field holds a single (atomic) value; a primary key exists |
| second (2NF) | 1NF, and no field depends on only part of a composite key |
| third (3NF) | 2NF, and no field depends on another non-key field |
A memory aid for 3NF: every field depends on "the key, the whole key, and nothing but the key".
Normalising an orders table
- Start: Order(OrderID, Date, CustomerID, CustomerName, ProductID, ProductName, Quantity), with a repeating group of products per order.
- 1NF: one row per product per order, key (OrderID, ProductID).
- 2NF: ProductName depends only on ProductID, and Date and CustomerID only on OrderID. Split into Order(OrderID, Date, CustomerID), Product(ProductID, ProductName) and OrderLine(OrderID, ProductID, Quantity).
- 3NF: CustomerName depends on CustomerID (not on OrderID), so move it to Customer(CustomerID, CustomerName).
Check yourself
A table has key StudentID and fields Tutor and TutorRoom, where TutorRoom depends on Tutor. Which normal form does it break?
Show the answer
Third normal form
Non-key depends on non-key.
4SQL: retrieving data
SQL (Structured Query Language) is used to define and query relational databases.
SELECT Name, Year
FROM Student
WHERE Year = 13
ORDER BY Name ASC;
Joining tables, using the foreign keys:
SELECT Student.Name, Course.Title, Enrolment.Grade
FROM Student
JOIN Enrolment ON Student.StudentID = Enrolment.StudentID
JOIN Course ON Course.CourseID = Enrolment.CourseID
WHERE Enrolment.Grade = 'A';
Useful extras: COUNT, SUM, AVG, MAX, MIN, GROUP BY, and LIKE with wildcards (Name LIKE 'S%' finds names starting with S).
Check yourself
Which keyword sorts the results?
Show the answer
ORDER BY
Ascending or descending.
5SQL: defining and changing data
CREATE TABLE Course (
CourseID CHAR(4) PRIMARY KEY,
Title VARCHAR(40) NOT NULL,
Credits INTEGER
);INSERT INTO Course (CourseID, Title, Credits)
VALUES ('CS01', 'Computer Science', 20);
UPDATE Course SET Credits = 30 WHERE CourseID = 'CS01';
DELETE FROM Course WHERE CourseID = 'CS01';
Always include a WHERE clause with UPDATE and DELETE: without one, every record is changed or deleted.
Check yourself
What happens if DELETE FROM Student; is run with no WHERE clause?
Show the answer
Every record in the Student table is deleted
No condition = all rows.
6Transactions and concurrent access
A transaction is a group of operations treated as one unit, such as moving money: take £50 from one account and add it to another. Transactions follow the ACID rules:
| Property | Meaning |
|---|---|
| atomicity | all of the transaction happens, or none of it |
| consistency | the database moves from one valid state to another |
| isolation | transactions running at the same time do not interfere |
| durability | once committed, changes survive a crash |
When many users share a database, two may try to change the same record at once and one update could be lost. Solutions:
- record locking: a record is locked while one user changes it;
- serialisation: transactions are run one after another;
- timestamp ordering and commitment ordering: the database decides the order using timestamps or dependencies.
Record locking can cause deadlock, where two users each wait for a record the other has locked.
Check yourself
What does atomicity mean for a bank transfer?
Show the answer
Either both the withdrawal and the deposit happen, or neither does
All or nothing.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera