9.1 Databases

← Topic 8.3 File handlingComputer Science contentsTopic 10.1 Standard logic gate symbols →
Chapter 9 · Databases

9.1 Databases

A database stores related data in a structured form so that information can be found and used efficiently. For IGCSE Computer Science, this topic focuses on single-table databases, suitable data types, primary keys, validation and SQL.

Single-table databasesFields & recordsData typesPrimary keysSQL

9.1.1 Single-table databases

A database is a structured collection of data that allows information to be extracted in a way that meets a user's needs. At IGCSE level, the focus is on a single-table database, which contains one table.

Why databases are useful

Databases can store information about people (such as patients or pupils), things (such as cars or books) and events (such as hotel bookings or race results).

Tables, records and fields

Data in a database is stored in a table. A table contains many records, and each record contains a fixed set of fields.

TermMeaningEasy way to remember
TableA collection of related records about one type of item, person or event.The whole grid.
RecordAll the data about one item, person or event.A row.
FieldOne specific item of data in a record.A column.
Figure 9.1 from the textbook showing records as rows and fields as columns in a database table

A table should have a meaningful name, such as PATIENT, BOOK or APPOINTMENT. Fields should also have meaningful names, normally written without spaces, such as FirstName, FamilyName, DateOfAdmission, Consultant, WardNumber and BedNumber.

Figure 9.2 from the textbook showing the structure of the PATIENT table

Validation in a database

Validation checks that input data is reasonable before it is accepted. Some checks can be provided automatically by database software, while others must be set up by the database developer.

For example, a date field can reject an invalid date automatically. A developer can also create a validation rule so that a ward number accepts only whole-number values from 1 to 10.

Figure 9.3 from the textbook showing automatic validation rejecting an invalid DateOfAdmission value
Figure 9.4 from the textbook showing a validation rule for WardNumber and an error message
Check tables, records, fields and validation.

9.1.2 Basic data types

Each field must be given a suitable data type. The data type controls how the value is stored and displayed and which operations can be performed on it.

IGCSE data typeWhat it storesExample
text/alphanumericA number of characters.Mr Smith
characterA single character.M
BooleanOne of two possible values, such as True/False or Yes/No.TRUE
integerA whole number.7
realA number containing a decimal/fractional part.12.75
date/timeA date and/or time.22/11/2022
Choose the type from the meaning of the field, not just the appearance of the value. For example, a telephone number may contain digits but is normally treated as text because arithmetic is not performed on it.

The source notes that database software may use different names for these types. For example, Microsoft Access uses names such as Short Text, Yes/No, Number and Date/Time.

Check your data-type choices.

9.1.3 Primary keys

Every record describes one item, person or event. To identify each record reliably, a table needs a field whose value is unique. This field is the primary key.

In the textbook PATIENT example, fields such as name, consultant, ward and bed can all contain repeated values, so an additional field called HospitalNumber is introduced. An example format is HN123456.

Exam point: when asked to choose a primary key, name the field and explain why its value uniquely identifies each record.
Check primary keys.

9.1.4 SQL

Structured Query Language (SQL) is the standard query language used to obtain useful information from a database. An SQL script is a list of SQL commands that performs a task and can be stored for reuse.

Core SQL commands

CommandPurpose
SELECTChooses the fields (columns) to display.
FROMIdentifies the table to use.
WHERESelects only records that match a condition.
ORDER BYSorts results alphabetically or numerically.
SUMReturns the total of values in a numeric field.
COUNTCounts records matching the query.

SELECT and FROM are the mandatory parts of the simple queries studied here. Other clauses are added when needed. A semicolon marks the end of the SQL command.

SELECT HospitalNumber, FirstName, FamilyName
FROM PATIENT
WHERE Consultant = 'Mr Smith';
Figure 9.5 from the textbook showing output for Mr Smith's patients

To sort the same results by family name:

SELECT HospitalNumber, FirstName, FamilyName
FROM PATIENT
WHERE Consultant = 'Mr Smith'
ORDER BY FamilyName;
Figure 9.6 from the textbook showing Mr Smith's patients in alphabetical order

Conditions and operators

Values in a condition must match the field's data type. Text and character values are enclosed in quotation marks in the source examples. Numeric values are written as numbers. The source also notes that date notation can differ between database systems.

OperatorMeaning
=equal to
>, <greater than, less than
>=, <=greater/less than or equal to
<>not equal to
BETWEENwithin a range
LIKEmatches a pattern
INmatches one of several values
ANDall conditions must be true
ORone or more conditions must be true
NOTthe condition must be false

ORDER BY, SUM and COUNT

ORDER BY FamilyName;
ORDER BY FamilyName DESC;
SELECT SUM(Badges)
FROM CUB;
SELECT COUNT(HospitalNumber)
FROM PATIENT
WHERE WardNumber = 7;
Check SQL commands and conditions.

Practical database case study

The book uses a Cub Scout database to show how a single-table database can be built from data requirements. The process is to identify the required fields, choose meaningful field names, assign suitable data types, add a primary key and set validation rules.

Figure 9.7 enrolment form from the textbook
Figure 9.8 blank database template from the textbook
Figure 9.9 creating the CubScout database from the textbook
Figure 9.10 design view from the textbook
Figure 9.11 naming the CUB table from the textbook
Figure 9.12 fields for the CUB table showing the primary key
Figure 9.13 validation rules for the Gender field

The Gender example combines several validation ideas: a presence check, a length check and a rule limiting the accepted format to the required values.

Apply database design ideas.

Extension: DDL and DML

The textbook extension introduces the industry distinction between Data Definition Language (DDL), which changes database structures, and Data Manipulation Language (DML), which works with the data stored in those structures.

AreaPurposeExamples from the source
DDLCreate or change the database structure.CREATE DATABASE, CREATE TABLE, ALTER TABLE, PRIMARY KEY
DMLAdd, change, delete or retrieve stored data.The query commands already studied, such as SELECT.

The extension also introduces SQL field types such as CHAR(n), VARCHAR(n), BOOLEAN, INTEGER, REAL, DATE and TIME.

Check the complete SQL section.

Topic 9.1 revision checklist

Define a database and a single-table database.
Distinguish tables, records and fields.
Explain why field and table names should be meaningful.
Explain the role of validation in a database.
Choose suitable text/alphanumeric, character, Boolean, integer, real and date/time data types.
Define a primary key and explain why its values must be unique.
Choose a suitable primary key from a set of fields.
Explain what SQL and an SQL script are.
Use and interpret SELECT, FROM, WHERE, ORDER BY, SUM and COUNT.
Use comparison and logical operators in SQL conditions.
Recognise ascending and descending ORDER BY queries.
Define a single-table database from given storage requirements.
Apply suitable data types, validation and a primary key to a table design.
Understand the extension distinction between DDL and DML.
Ready for a mixed Topic 9.1 check?
← Topic 8.3 File handlingComputer Science contentsTopic 10.1 Standard logic gate symbols →