🎯 Syllabus & Goals 3 min
Cambridge 9.1 · Basic data types Cambridge 9.1 · Primary keys Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Name and describe the six database data types: text/alphanumeric, character, Boolean, integer, real and date/time.
- Choose, with a reason, a suitable data type for each field in a table.
- Identify a suitable primary key, or add one, and justify the choice.
Textbook: Chapter 9, §9.1.2–9.1.3 (pp. 342–344)
Recap / Warm-Up 5 min
Last lesson: a table holds records (rows), and each record has the same fields (columns). Validation keeps unreasonable data out.
Quick starter
In the PET table, which two fields contain repeated values that make them useless for telling pets apart?
| PetID | PetName | Species | OwnerSurname | DateOfBirth | Sex | WeightKg | Vaccinated |
|---|---|---|---|---|---|---|---|
| P101 | Biscuit | Dog | Taylor | 2019-04-12 | M | 12.5 | TRUE |
| P102 | Luna | Cat | Brown | 2021-09-30 | F | 4.2 | TRUE |
| P103 | Pepper | Rabbit | Taylor | 2022-02-14 | F | 1.8 | FALSE |
| P104 | Max | Dog | Novak | 2016-07-03 | M | 30.1 | TRUE |
| P105 | Luna | Dog | Garcia | 2020-11-22 | F | 8.7 | FALSE |
| P106 | Kiwi | Parrot | Kim | 2018-05-09 | M | 0.4 | TRUE |
Reveal the answer
PetName (two pets called Luna) and OwnerSurname (two Taylors). Species and Sex repeat too.
🧠 Key Concept 14 min
1 · What is a data type?
Every field must be given a data type. The data type decides how a value is stored, how it is displayed, and what can be done with it. An integer field, for example, can be used in calculations.
2 · The six basic data types
| Data type | Holds | Good for |
|---|---|---|
| Text / alphanumeric | A number of characters: letters, digits, symbols | Names, addresses, codes like P101, phone numbers |
| Character | A single character | A grade A, a size S/M/L |
| Boolean | One of two values: TRUE/FALSE, Yes/No, 1/0 | Paid?, In stock?, Member? |
| Integer | A whole number | Number in stock, pages, age in years |
| Real | A number with a decimal part | Price, weight, height in metres |
| Date/time | A date and/or a time | Date of birth, appointment time |
3 · Primary keys
We must be able to pick out one record, and only one. So one field must hold a value that is never repeated in the table. That field is the primary key.
- An existing field can be the key if it is always unique — for example a book's ISBN.
- If every existing field could repeat, add a new field, such as a unique ID number.
PetID is guaranteed never to repeat, so it is the primary key. Repeated values are shaded red.Why does it matter? Search the PET table by name and you cannot tell which Luna you mean:
SELECT PetID, PetName, OwnerSurname FROM PET WHERE PetName = 'Luna';
| PetID | PetName | OwnerSurname |
|---|---|---|
| P102 | Luna | Brown |
| P105 | Luna | Garcia |
Two records come back. The primary key tells them apart: P102 and P105. (You will write queries like this from Lesson 3.)
Worked Example 12 min
(a) Choosing a data type for every field
Choose the data type for each PET field and give a reason. This is how a 1-mark-per-field question is answered.
PetID→ text. It mixes a letter and digits (P101).PetName,Species,OwnerSurname→ text. Each holds several letters.DateOfBirth→ date/time. It is a date; the software can then check it is a real date and sort it in date order.Sex→ character. Always exactly one letter, M or F.WeightKg→ real. Weights have decimal parts, such as 12.5.Vaccinated→ Boolean. Only two answers are possible: TRUE or FALSE.
(b) Choosing a primary key
ModelCode (e.g. ZX-0042, one per model), Brand, ScreenSize, Colour, Price, InStock.- Test
Brand: many models share a brand → repeats → not suitable.A key must never repeat. - Test
Colour,ScreenSize,Price,InStock: all can repeat → not suitable.Two phones can cost the same or be the same colour. - Test
ModelCode: one code per model → unique → suitable.It uniquely identifies each record.
Answer: ModelCode (1) because each model has a different code, so it uniquely identifies each record (1).
Try It Yourself 12 min
Goal: Give the data type for: (a) number of pages in a book, (b) a shirt size S/M/L, (c) a train's departure time, (d) a price of 4.75.
Goal: A library BOOK table has fields ISBN, Title, Author, Pages, OnLoan. Give each field a data type with a reason, and state the primary key.
Goal: A school club table stores FirstName, FamilyName, Year, DateJoined. None is unique. Design a new primary-key field: give its name, data type, format and a validation rule.
Hint
Think of a code such as two letters plus four digits. Which validation check makes sure every new value follows that pattern?
📝 Exam Practice 10 min
Define the term primary key.
Mark scheme
- A field that uniquely identifies each record in a table (1).
A bike-hire shop keeps a table, BIKE. Complete the data type for each field.
| Field | Example | Data type |
|---|---|---|
BikeCode | BK0142 | ………… |
FrameSize | M | ………… |
HourlyRate | 3.50 | ………… |
Gears | 21 | ………… |
Electric | FALSE | ………… |
Mark scheme
- BikeCode — text / alphanumeric (1).
- FrameSize — character (1).
- HourlyRate — real (1).
- Gears — integer (1).
- Electric — Boolean (1).
State, with a reason, which field in the BIKE table should be the primary key.
Mark scheme
- BikeCode (1).
- It is unique for each bike // no two records have the same value (1).
A student says the field PhoneNumber should have the data type integer. Explain why text is a better choice.
Mark scheme
- A phone number can start with 0, which an integer would lose (1).
- It may contain characters such as +, spaces or brackets (1).
- No calculations are ever done with it, so it does not need to be numeric (1).
Recap & Key Terms 3 min
Every field has one of six data types. Pick the type from what the data is and how it will be used. One field — the primary key — must be unique for every record; add an ID field if none is.
- Data type
- A classification of how data is stored and displayed, and which operations can be performed on it.
- Text / alphanumeric
- A number of characters (letters, digits, symbols).
- Character
- A single character.
- Boolean
- One of two values, e.g. TRUE/FALSE.
- Integer / real
- A whole number / a number with a decimal part.
- Primary key
- A field that uniquely identifies each record in a table.
Homework 1 min
Task (≤ 15 min): A theatre table, SHOW, stores for each performance: a show number such as SH104, the type of show, its title, the date, the ticket price and whether it is sold out. (a) Give a data type for each field. (b) State, with a reason, the primary key.
Model answer
- Show number — text (letters and digits); type — text; title — text; date — date/time; price — real; sold out — Boolean.
- Primary key: the show number, because each performance has a different number, so it is unique.