Structure of a database
IGCSE Computer Science · Databases
Table, record, field
| table | the whole set of data, arranged in rows and columns |
| record | all the data about one item — one row |
| field | one item of data — one column |
In a table of pupils, one record holds everything about one pupil; the field called $\text{Surname}$ holds the surname of every pupil.
Confusing the two is the commonest error in this topic. The fix is to picture the table: records go across, fields go down.
The primary key
A primary key is a field that uniquely identifies each record in the table. No two records may have the same value in it, and it must not be left empty.
Why it is needed: without a unique identifier, the database has no reliable way to pick out one specific record. Two pupils named the same would be indistinguishable.
Choosing a good primary key
A good primary key is unique, never empty, and never changes.
That last requirement rules out most obvious choices:
- surname — not unique, and people do change their names;
- date of birth — very far from unique;
- telephone number — unique at any moment, but people change numbers, and a changed key breaks every link to that record;
- email address — same problem.
This is why organisations invent an ID number that means nothing outside the system: a pupil number, an account number, a membership number. Because it carries no real-world meaning, there is never any reason to change it.
Data types in a database
| text / alphanumeric | letters and digits — names, addresses, telephone numbers |
| character | a single letter or symbol |
| Boolean | a yes/no field — has the fee been paid? |
| date / time | a date or a time, so the database can sort and compare them properly |
| integer | a whole number — a quantity in stock |
| real | a number with decimals — a price |
Note the telephone number again: it is text, not a number, because a numeric field would drop the leading zero and because nobody ever performs arithmetic on a phone number.
Note also why a date field is worth using rather than text: with a real date type, the database can sort dates in order and work out the difference between two of them. Stored as text, $\text{"01/12/2025"}$ sorts before $\text{"02/01/2025"}$, which is wrong.
Validation in a database
The same checks as in programming can be attached to a field: range, length, type, presence, format. Setting these once on the field means every entry is checked, no matter who types it.
Worked example: choosing a primary key for a student table
A school builds a table of student data with the fields: full name, date of birth, parent's telephone number, address, student ID. (a) State three requirements of a good primary key. (b) Consider each field and explain which can be used. (c) State the data type for each field.
Solution
(a) Three requirements of a good primary key.
- unique — no two records have the same value;
- never empty — every record must have a value;
- never changes — the value must stay stable for the whole life of the record.
(b) Consider each field.
Full name → cannot be used. It breaks both of the important requirements: two students with the same name is very common, and people also change their names.
Date of birth → cannot be used. It is very far from unique: in a large school, dozens of students are born on the same day.
Parent's telephone number → cannot be used. This field looks fine at first glance because it is usually not duplicated, but it fails in two ways.
First, siblings share the same parent's telephone number — so it is not unique.
Second, and more seriously: people change their telephone number. When a primary key changes, every link to that record — marks, attendance, fees — is broken.
Address → cannot be used. Siblings live in the same house, and families move.
Student ID → CAN be used. It is the only field that meets all three requirements.
Why it qualifies. The school invents this ID and guarantees that it is not duplicated. More importantly, it has no meaning outside the system — it is no one's name and no one's house number.
That is exactly why there is never any reason to change it. Students change their names, their homes, their telephone numbers — the student ID stays the same.
That is the general principle: organisations invent a meaningless ID number to serve as the primary key for precisely this reason.
(c) The data type for each field.
- full name → text;
- date of birth → date/time, so that the database can sort and calculate ages;
- parent's telephone number → text, because a numeric type throws away the leading 0 and nobody calculates with telephone numbers;
- address → text;
- student ID → text if it contains letters such as $\text{HS001}$; integer if it is made up of digits only.
Worked example: why the date type differs from the text type
Someone stores dates of birth as text instead of the date type. Explain two things the database will not be able to do correctly, with concrete examples.
Solution
The first thing — sorting in time order.
When a field has the text type, the database sorts it in alphabetical order, that is, it compares character by character from left to right.
A concrete example. The two dates $\text{"01/12/2025"}$ and $\text{"02/01/2025"}$.
In real time, $\text{01/12/2025}$ (month $12$) comes after $\text{02/01/2025}$ (month $1$).
But when compared as text, the machine reads the first character: $\text{"0"}$ equals $\text{"0"}$. Then the second character: $\text{"1"}$ is smaller than $\text{"2"}$. It stops right there and concludes that $\text{"01/12/2025"}$ comes first.
The result: the list is sorted completely wrongly, because the machine sorts by day first and only then by month and year.
With a real date type, the database understands the three parts as day, month and year and sorts correctly by time.
The second thing — calculating the interval between two dates.
With a date type, the database can subtract one date from another to get the number of days or years between them. That lets it calculate a student's age from the date of birth, or filter out students over $16$ years old.
With a text type, this cannot be done. To the machine, the string $\text{"14/03/2009"}$ is just ten characters, not a point in time. Subtracting one text string from another is a meaningless operation.
To calculate an age, the user would have to split the string apart into three parts, convert them to numbers, and calculate it themselves — long-winded and error-prone.
The principle to draw. Choose a data type not just to store a value, but so that the database can understand the meaning of that value.
The same reasoning explains why a price should be a real number (so it can be added and averaged) — but a telephone number should be text, because we never calculate with it, and we need to keep the leading 0.