Structure of a database

IGCSE Computer Science · Databases

Table, record, field

tablethe whole set of data, arranged in rows and columns
recordall the data about one item — one row
fieldone 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:

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 / alphanumericletters and digits — names, addresses, telephone numbers
charactera single letter or symbol
Booleana yes/no field — has the fee been paid?
date / timea date or a time, so the database can sort and compare them properly
integera whole number — a quantity in stock
reala 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.

A record runs ACROSS — a field runs DOWN MaHS HoTen NgaySinh DaDongPhi HS001 Lan 14/03/2009 Correct HS002 Minh 02/09/2009 Sai HS003 Minh 21/11/2008 Correct ← ONE RECORD (one row) ONE FIELD (one column) StudentID is the PRIMARY KEY: unique, not empty, never changes. FullName CANNOT be used as the key — HS002 and HS003 share a name, and people also change their names.
A student ID is invented and has no meaning outside the system — which is exactly why it never has any reason to change.

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.

(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.

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.

Common mistake

Choosing a full name or a telephone number as the primary key. Both look as if they identify a person, so intuition picks them. But a primary key must satisfy three things: unique, never empty, and never changing. A full name fails at the very first — two people with the same name is very common. A telephone number fails at the third, and this is the dangerous spot: it seems unique so it slips through easily, but people change their number — and when a primary key changes, every link to that record is broken. That is why organisations invent an ID number that has no meaning outside the system: a student ID, an account number, a membership card number. Precisely because it is meaningless in the real world, there is never any reason to change it. The accompanying trap: storing a date of birth as the text type. The database then sorts in alphabetical order, so $\text{"01/12/2025"}$ comes before $\text{"02/01/2025"}$ — completely wrong — and it also cannot calculate ages.

Key takeaway

A record is a row — all the data about one object; a field is a column — one item of data for every object. A primary key must be unique, not empty, never changing, so full names and telephone numbers cannot be used; organisations invent a meaningless ID number for exactly that reason. Choose data types so the database can understand the meaning: date/time to sort and calculate ages correctly; text for telephone numbers because a numeric type throws away the leading 0. Attach validation to a field and every entry is checked.

← Arrays, subroutines and files · All IGCSE Computer Science lessons · Practice IGCSE Computer Science questions (free, instant scoring) · Querying with SQL →