OCR H446, section 1.3.2 (a)
A database is an organised collection of data that can be stored, searched and updated. This page covers the basic ideas and terms, and how to model the data before building a database.
Flat file databases
A flat file database stores everything in a single table (like a spreadsheet), with one row for each record.
| LoanID | Member | Phone | Book | Author | Date |
|---|---|---|---|---|---|
| 1 | Asha Khan | 07700 900111 | Dune | F. Herbert | 2026-11-02 |
| 2 | Asha Khan | 07700 900111 | Emma | J. Austen | 2026-11-09 |
| 3 | Ben Ortiz | 07700 900222 | Dune | F. Herbert | 2026-11-10 |
Problems:
- Data redundancy: Asha's phone number and the details of Dune are repeated, which wastes space.
- Inconsistency: change a phone number in one row but not another, and the data contradicts itself.
- Anomalies when inserting, updating and deleting data (see 1.3.2 (c)).
- Hard to search and report on well.
A flat file is fine for small, simple data, such as a short contact list.
Relational databases
A relational database splits the data into several tables (relations), each about one type of thing, and links them using keys. Each fact is stored once.
MEMBER
| MemberID | Name | Phone |
|---|---|---|
| 1 | Asha Khan | 07700 900111 |
| 2 | Ben Ortiz | 07700 900222 |
BOOK
| BookID | Title | Author |
|---|---|---|
| 10 | Dune | F. Herbert |
| 11 | Emma | J. Austen |
LOAN
| LoanID | MemberID | BookID | Date |
|---|---|---|---|
| 1 | 1 | 10 | 2026-11-02 |
| 2 | 1 | 11 | 2026-11-09 |
| 3 | 2 | 10 | 2026-11-10 |
| Term | Meaning |
|---|---|
| Table (relation) | Data about one type of entity |
| Record (row, tuple) | One instance, such as one member |
| Field (attribute, column) | One property, such as Name |
Advantages over a flat file
- Less redundancy and less storage used.
- More consistent: each fact is changed in one place.
- Easier to maintain and extend.
- Powerful searching across tables with SQL.
- Better security: different users can be given access to different tables.
Keys
Primary key
A primary key is a field (or set of fields) whose value uniquely identifies each record in a table. It must be unique and never empty. MemberID is the primary key of MEMBER. A key made of more than one field is a composite key.
Foreign key
A foreign key is a field in one table that is the primary key of another table, used to link the two. MemberID and BookID in LOAN are foreign keys, which link each loan to the member and the book.
Secondary key
A secondary key is a field that isn't the primary key, but that is used to search or sort the data, usually because it is searched often. For example, Name or Title. A secondary key need not be unique: several members could have the same surname. It is often given an index.
Indexing
An index is a separate data structure that makes searching faster, like the index at the back of a book. It holds the values of a field in order, with a pointer to the full record. The database can search the sorted index quickly (for example with a binary search), instead of checking every record.
- Advantage: searches on that field are much faster, especially in big tables.
- Disadvantage: it takes extra storage, and must be updated whenever records are added, changed or deleted, which slows down writing.
Indexes are normally created on primary keys and on secondary keys that are searched often.
Entity relationship modelling
Before building a database, it is designed with an entity relationship (ER) diagram.
- An entity is a thing that data is stored about (a member, a book). Each becomes a table.
- An attribute is a property of an entity.
- A relationship is a link between two entities.
Types of relationship
| Type | Meaning | Example |
|---|---|---|
| One-to-one (1:1) | One of A links to at most one of B | A person and their passport |
| One-to-many (1:M) | One of A links to many of B | One member has many loans |
| Many-to-many (M:M) | Many of A link to many of B | Members and books (over time) |
On an ER diagram, entities are drawn as boxes and relationships as lines, with a "crow's foot" at the "many" end.
A many-to-many relationship can't be stored directly in a relational database. It is resolved by adding a link entity (here LOAN) between the two, which turns it into two one-to-many relationships. The link entity holds the foreign keys of both, and can hold extra data such as the date.
Worked example
Task: a school clinic records visits. Each pupil can visit many times, and each visit is seen by one nurse. Design the entities and relationships, and identify the keys.
- Entities:
PUPIL,NURSE,VISIT. - Relationships: one pupil has many visits (1:M), and one nurse sees many visits (1:M).
VISITis the "many" side of both. - Tables and keys:
PUPIL (PupilID, Name, Form)
NURSE (NurseID, Name)
VISIT (VisitID, PupilID → PUPIL, NurseID → NURSE, Date, Reason)
The primary key is underlined, and each foreign key is followed by an arrow to the table it links to.
- A secondary key, such as
DateinVISIT, could be indexed if staff often search by date.
Exam tip: define each key precisely: primary (uniquely identifies a record), foreign (the primary key of another table, used to link), secondary (used for searching, and not necessarily unique). For many-to-many, say it is resolved with a link entity.
Check yourself
Give two problems with a flat file database
Any two of: redundant (repeated) data; inconsistent data when only some copies are changed; anomalies when inserting, updating or deleting; difficult to search and report on.
What is the difference between a primary key and a foreign key?
A primary key uniquely identifies each record in its own table. A foreign key is a field in a table that is the primary key of another table, and links the two.
What is a secondary key?
A field, other than the primary key, that is used to search or sort the data, often with an index. It doesn't have to be unique.
How is a many-to-many relationship stored in a relational database?
By adding a link entity (table) between the two, holding the foreign keys of both, which gives two one-to-many relationships.