Single-Table Database Design
1. What a Database Is
A database is a structured, organised collection of data stored so that it can be searched, sorted and updated easily. Storing the same data in a plain text file or a pile of paper forms makes it slow to search and easy to corrupt.
2. Essential Terminology
Three terms describe the structure of a single table, and they are examined directly.
| Term | Meaning | In a table |
|---|---|---|
| Field | A single item of data, of one specific data type | A column |
| Record | A complete set of fields about one person, object or event | A row |
| Primary key | A field that uniquely identifies each record | One designated column |
3. The Primary Key
The purpose of a primary key is to identify each record uniquely, so that no two records can be confused and any single record can be found reliably.
A suitable primary key must:
- be different for every record — no duplicates are possible
- never be left empty
- not change over time
Choosing a primary key
Consider a table of students. Which field would work?
| Field | Suitable? | Why |
|---|---|---|
| FirstName | No | Two students can share a first name |
| Surname | No | Duplicates are very likely |
| DateOfBirth | No | Several students may share a birthday |
| ClassGroup | No | Many students are in the same class |
| StudentID | Yes | Deliberately issued so that it is unique and never reused |
4. Basic Data Types
Each field is given a data type. The syllabus limits these to six.
| Data type | Stores | Example |
|---|---|---|
| Text / alphanumeric | Letters, digits and symbols together | 14 Green Lane |
| Character | A single character only | M |
| Boolean | One of two values only | TRUE / FALSE |
| Integer | A whole number | 17 |
| Real | A number with a decimal part | 72.5 |
| Date/time | A calendar date and/or a time | 14/07/2009 |
5. Validation in a Database
A field definition can also carry validation so that impossible data is refused as it is entered. The same checks used in programming apply here.
| Field | Suitable validation |
|---|---|
| Age | Range check — between 11 and 19 |
| StudentID | Length check — exactly 6 characters; presence check |
| Surname | Presence check — must not be blank |
| DateOfBirth | Format check — dd/mm/yyyy |
6. Designing a Single Table
Given a set of storage requirements, a database definition states each field name, its data type, and which field is the primary key.
Worked example
A school library needs to store, for each book: a unique reference, the title, the author, the year of publication, the price paid, and whether it is currently on loan.
| Field name | Data type | Notes |
|---|---|---|
| BookRef | Text/alphanumeric | Primary key — unique for every book |
| Title | Text/alphanumeric | |
| Author | Text/alphanumeric | |
| YearPublished | Integer | A whole number, used in comparisons |
| Price | Real | Needs a decimal part |
| OnLoan | Boolean | Only two possible values |
With sample data the table looks like this:
| BookRef | Title | Author | YearPublished | Price | OnLoan |
|---|---|---|---|---|---|
| LIB001 | Hard Times | Dickens | 1854 | 7.99 | TRUE |
| LIB002 | Silas Marner | Eliot | 1861 | 6.50 | FALSE |
| LIB003 | Kim | Kipling | 1901 | 8.25 | FALSE |
Each column is a field. Each row is a record. BookRef is the primary key, so no two rows can ever be mistaken for one another.
7. Exam Focus
Quick self-check
- Define field, record and primary key.
- State the purpose of a primary key in one sentence.
- Explain why a mobile phone number should be stored as text, not integer.
- A club stores, for each member: a membership number, name, date joined, fees paid in the year, and whether they are a committee member. Give a suitable data type for each and identify the primary key.
- Suggest a suitable validation check for a field storing a percentage mark.