Note AcademyNote Academy

Computer Science 2210 · Chapter 9

Databases

Fields, records and primary keys in a single-table database, the data types, and the SQL commands SELECT, FROM, WHERE, ORDER BY, SUM and COUNT.

Written for the 2023–2025 syllabus · 4 min read

Single-table database

A database is a structured collection of data that allows people to extract information in a way that meets their needs. A single-table database contains only one table.

Fields

Fields represent the individual columns in the table, each storing a specific type of information. For example:

  1. StudentID
    • Data type: integer
    • Description: a unique identifier for each student
    • Validation: must be unique and not null

Records

Records represent the individual rows in the table, each containing data for a specific student.

StudentIDNameAge
1Hugh Jackman7
2Bruce Wayne8
3Peter Parker7

In this table, Name is a field (a column) and the row for StudentID 2 is a record.

A Student table with the Name field, the second record and the StudentID primary key marked

A Student table with the Name field, the second record and the StudentID primary key marked

Validation

Validation has already been discussed in Chapter 7.

Data Types

Data types are categories of data that tell the computer how to interpret the values stored in variables and how much space to allocate for them in memory. The table below shows the data types included in your syllabus.

Data typeDescription
Text / alphanumericA number of characters
CharacterA single character
BooleanOne of two values: either True or False, 1 or 0, Yes or No
IntegerWhole number
RealDecimal number
Date / timeDate and time

Primary Keys

A primary key is a unique identifier for each record in a database table. It ensures that each record can be uniquely identified and accessed, which is crucial for maintaining data integrity and enabling efficient data retrieval.

For example, in the student table above, StudentID will be used as the primary key for that table because it is unique, cannot be null (empty) and cannot be changed.

Structured Query Language (SQL)

Structured Query Language (SQL) is the standard query language for writing scripts to obtain useful information from a database. The single-table database below is what we will be using in the examples ahead.

Table name: ItemList

ItemIDItemNamePrice
254Apple20
255Coffee100
256Chocolate50
257Bread20

SELECT and FROM

The SELECT and FROM commands are mandatory to use, while the rest are optional.

sql
SELECT ItemID, ItemName
FROM ItemList

Result:

ItemIDItemName
254Apple
255Coffee
256Chocolate
257Bread

From the example above, we can deduce that:

  • the SELECT command is used to select the columns.
  • the FROM command specifies the table to be used within the database.

To select all the columns in the table, use this command:

sql
SELECT *
FROM ItemList

WHERE

The WHERE command is used to specify a condition.

sql
SELECT ItemName, Price
FROM ItemList
WHERE ItemID = 255

Result:

ItemNamePrice
Coffee100

ORDER BY

The ORDER BY command organises the data in ascending or descending order. The example below orders the table in ascending order of price.

sql
SELECT *
FROM ItemList
ORDER BY Price

Result:

ItemIDItemNamePrice
254Apple20
257Bread20
256Chocolate50
255Coffee100

To order it in descending order, use this command:

sql
SELECT *
FROM ItemList
ORDER BY Price DESC

SUM

The SUM statement is used to sum all integer or real values in a field.

sql
SELECT SUM(Price)
FROM ItemList

Result:

SUM(Price)
190

COUNT

The COUNT statement counts all the records in a field.

sql
SELECT COUNT(ItemID)
FROM ItemList
WHERE Price = 20

Result:

COUNT(ItemID)
2

An SQL query with SELECT, FROM, WHERE and ORDER BY applied to the ItemList table, giving the Apple and Bread rows

An SQL query with SELECT, FROM, WHERE and ORDER BY applied to the ItemList table, giving the Apple and Bread rows
Test yourself12 flashcards on this chapter.Flashcards →

Practise this chapter

Every past-paper question on these topics, with the mark scheme.