9.1 Database Structure
A database is an organised collection of related data that can be searched, sorted and updated quickly. A database management system (DBMS) is the software that creates and controls the database, provides the user interface, enforces validation and access rights, and allows queries to be run.
| Term | Meaning |
|---|---|
| Table / file | A collection of related records, e.g. Students |
| Record | One complete row — all the data about one person or item |
| Field | One column — a single attribute, e.g. Surname |
| Primary key | A field that uniquely identifies each record; it cannot be duplicated or left empty |
| Foreign key | A field in one table that links to the primary key of another table |
| Data dictionary | A description of every field: name, data type, length, validation rules |
9.2 Flat File and Relational Databases
| Flat file | Relational | |
|---|---|---|
| Structure | One single table | Two or more linked tables |
| Redundancy | Data is repeated in many records | Data is stored once and linked by keys |
| Problems | Wasted storage, inconsistency when a repeated value is updated in one place only, harder to maintain | More complex to design, but consistent and easier to update |
| Example | A single table holding student name, class and teacher for every row | Students table + Classes table linked by ClassID |
Splitting data into related tables removes redundancy (the same data stored many times) and prevents inconsistency (the same data stored with different values in different places).
9.3 Data Types and Validation
| Data type | Use |
|---|---|
| Text / alphanumeric | Names, addresses, postcodes |
| Number (integer / real) | Age, price, quantity |
| Date / time | Date of birth, appointment time |
| Boolean | Yes/no fields such as Paid |
| Currency | Money values |
Validation rules on a field include: range check (e.g. 0–100), length check (exactly 8 characters), presence check (must be filled in), type check (must be a number), format check (e.g. DD/MM/YYYY) and lookup check (must be one of a list of values).
9.4 SQL
SQL (structured query language) is used to search and manipulate data in a relational database.
| Clause | Purpose |
|---|---|
SELECT | Which fields to display (* for all) |
FROM | Which table(s) to use |
WHERE | Which records to include |
ORDER BY | Sort the results (ASC or DESC) |
GROUP BY | Group records so totals can be calculated per group |
| Function | Returns |
|---|---|
COUNT | The number of records |
SUM | The total of a numeric field |
AVG | The average of a numeric field |
MAX / MIN | The largest / smallest value |
Exam trap
Text values in SQL need quotation marks (WHERE Class = "10A"); numbers do not. Forgetting the quotes, or using = instead of AND for two conditions, are the most common SQL errors.
Five-minute drill
- Define primary key and foreign key.
- Give two problems caused by storing data in a flat file.
- Write an SQL query to list all students with a mark above 80, highest first.
- Name three validation checks and an example of each.
- State two functions of a DBMS.
A school stores student data in a flat file. Explain two problems with this approach and how a relational database solves them.
- Redundancy: the same data (e.g. class teacher) is repeated in every record, wasting storage (1). In a relational database it is stored once in a linked table (1).
- Inconsistency: if a repeated value is updated in one record only, the data disagrees (1). With linked tables there is only one copy to update, so the data stays consistent (1).
A table Results has fields StudentID, Surname, Subject, Mark. Write an SQL query to display the Surname and Mark of all students who scored more than 70 in Physics, in descending order of Mark.
SELECT Surname, Mark(1)FROM Results(1)WHERE Subject = "Physics" AND Mark > 70(2 — one mark for each condition, quotes required for text)ORDER BY Mark DESC(1)