7 - Relational Databases and SQL
7 - Relational Databases and SQL
7 - Relational Databases and SQL
net
The database files are in .db format which will work with SQLite which is
free to use at https://sqliteonline.com/ and an installable tool (DB4S) can
be downloaded for free from http://tiny.cc/sqldb
The SQL files are just text files with a .sql extension so they can be imported
into SQLite or any other database software.
The files are also available in Microsoft Access format where the tables will
be stored as tables and the SQL will be stored as queries. There is also an
excellent SQL tutorial that you can follow at http://tiny.cc/sqltutor
Database terminology
From the AQA specification:
“Note that whilst the terms entity, attribute and entity identifier are more
commonly used when an abstract model of a database is being
considered, the terms given here will be used for both implementations of
and abstract models of databases.”
Contents
1) Databases ................................................................................................................... 3
Database structure ................................................................................................. 3
Table .............................................................................................................. 3
Record........................................................................................................... 4
Field ............................................................................................................... 4
Primary Key ................................................................................................... 5
Relational databases ............................................................................................. 7
Foreign Key ................................................................................................... 7
One to one relationships ............................................................................. 8
One to many relationships ........................................................................ 10
Advantages of relational databases ................................................................. 14
Redundant data ........................................................................................ 14
Inconsistent data ....................................................................................... 14
Eliminating redundant data and inconsistency...................................... 15
Advantages of eliminating redundant data .......................................... 17
2) SQL ............................................................................................................................. 18
Selecting data ...................................................................................................... 18
Select fields ................................................................................................. 18
Ordering data ............................................................................................ 19
Select records............................................................................................. 20
Wild cards ................................................................................................... 22
IN .................................................................................................................. 23
BETWEEN ...................................................................................................... 24
Multiple tables ............................................................................................ 26
Inserting data ........................................................................................................ 29
Updating data ...................................................................................................... 31
Deleting data ........................................................................................................ 33
1) Databases
Database structure
Databases are a structured method of storing data. Data is stored in tables and
each set of data about one thing is known as a record. Each category of
information is known as a field. Tables can have fields with different data types,
unlike an array, and the fields can be referenced by a field name instead of a
number. Records can also be added to, or deleted from, a table, unlike an array.
When looking at a table, records are in rows and fields are in columns. Each field is
identified by a fieldname.
The rows are records, such as the record for BA55WEP which is a Volkswagon
Beetle.
The table above is a flat file (single table) database. You will find out about
problems that can be caused by flat files later in this chapter.
Table
A table stores data about things that exist. The example above is a table that
stores data about cars. Things that exist can be:
• people (e.g. driving instructors)
• places (e.g., towns)
• objects (e.g. cars)
• events (e.g. lessons)
Tables have a structure that includes rows (records) and columns (fields). Each
table must have:
• a unique name (e.g. CARS)
• unique records (each record must be unique and not repeated)
• unique fields (each field must have a unique field name)
© paullong.net 2021 Page 3 of 35 by Paul Long
The Ultimate GCSE CS Textbook for AQA – Chapter 7 Published by paullong.net
Record
Each record stores data about an individual thing that exists. These things could
include a:
• person
• place
• object
• event
Notice how these are singular occurrences of the group names used to name
tables. Each record is a row within a table and each record must be unique within
the table - it must not be repeated.
Example – car
There are 6 records in this table below. Two of those records have been
highlighted and each record is for an individual car – a single thing that exists.
One of the records is for a Mini Cooper with a registration number of BC53PRS.
Field
A field is a category of information. Imagine the phrase ‘field of study’ which
means ‘area of study’. Fields are columns within a table and have titles which are
known as field names.
Example
Two fields are highlighted below. They have field names of Make and Reg Year
which are categories of information about the cars. Ford, Mini and 2003 are
examples of data items.
Data items are the individual items of data for each field and record. They are
found where a field (column) and record (row) meet. Each field must have a
unique field name so that no two fields within the same table have the same field
name. Data types can be set for each field such as text, number and date/time.
Validation can be applied to fields so that data must meet certain criteria to be
accepted.
© paullong.net 2021 Page 4 of 35 by Paul Long
The Ultimate GCSE CS Textbook for AQA – Chapter 7 Published by paullong.net
Primary Key
A primary key is a field that is a unique identification for each record. Within the
primary key field, each value can only appear once.
It is good practice to always use some sort of ID or Code that is unique and will not
change. In the table above, a Customer ID field would be an appropriate primary
key.
In the first table above, VIN is used as the primary key instead of registration
because the registration number could be changed. In the second table, an ID
number is used instead of Course_Title because in the future additional courses
could be added that have the same names as existing courses.
Relational databases
Relationships exist between records within tables. A relationship is between a
record from one table and a record(s) from another table. There are 2 types of
relationships that can be created:
• One to One
• One to Many
Example - relationships
One to One - e.g. one CAR has one INSURANCE_POLICY
INSTRUCTOR LESSON
Foreign Key
A foreign key is a field in one table that links to a primary key in another table using
a relationship.
CAR INSURANCE_POLICY
VIN PolicyNumber
Registration Type
Make Start Date
Model End Date
VIN
One CAR has one INSURANCE_POLICY and one INSURANCE_POLICY has one CAR.
The foreign key from one table links to the primary key of another table.
INSURANCE_POLICY CAR
HUSBAND WIFE
COUNCIL MAYOR
PERSON BAPTISM
For the first two relationships, assume that data is not stored about previous
marriages or previous mayors.
STUDENT
TUTOR GROUP Student Number
Tutor Group Name Surname
Classroom Forename
Teacher Date of Birth
Tutor Group Name
One TUTOR group has many STUDENTS but each STUDENT has one TUTOR group
making this a one to many relationship.
The foreign key (Tutor Group Name) from the STUDENT table links to the primary key
(Tutor Group Name) of another table (TUTOR GROUP).
There is a rule for determining which side of the one to many relationship the
foreign key will go on. The foreign key always goes on the many side of the
relationship so that it points to one single record in the other table:
If the foreign key is put on the wrong side of the relationship (the one side), then it is
impossible to store the related data properly because it will result in non-atomic
data (multiple data items in one field for a record):
PET OWNER
VEHICLE LESSON
SCHOOL TEACHER
DIRECTOR FILM
A table called DIVERS already exists. A table called DIVES will be used to store
information about each diver’s dives.
Redundant data
Redundant data can occur when relationships have not been set up correctly.
Data is duplicated. The data exists more than once, so it is classed as being
redundant (not needed).
Each department only has one manager and we can see that the Sales
department is managed by John Marshall and the Marketing department is being
managed by Fred Parkes. Three of these employees work in the same
department. The information about John Marshall being the manager for the sales
department has been repeated twice and this is unnecessary. This makes the
duplicated data about John Marshall redundant (not needed).
Inconsistent data
The redundant data has also led to the marketing department being incorrectly
called the Advertising department for Amy Harper. A search for employees in the
Marketing department would only return Bill Goaty and not Amy Harper.
EMPLOYEE DEPARTMENT
Note: eliminating inconsistent data does not eliminate errors. For example, if
Marketing had been spelt “Marketting” in the DEPARTMENT table then it
would consistently incorrect.
Questions – follow me
1) The tables below store data about books and their authors. Assume that each
book only has one author.
BOOKS AUTHORS
Book_ID Title Author_ID Author_ID Forename Surname
1 The Twits 3 1 Anna Sewell
2 Black Beauty 1 2 Charles Dickens
3 A Christmas Carol 2 3 Roald Dahl
4 Oliver Twist 2
5 The BFG 3
a) Using the example above, describe the purpose of a primary key. [2]
b) Using the example above, describe the purpose of a foreign key. [3]
c) Using the example above, suggest another field for the table BOOKS. [1]
d) Draw or describe the relationship between BOOKS and AUTHORS. [1]
2) SQL
Selecting data
Asking a question of a database is known as a select query:
SELECT <fieldnames>
FROM <table name>
WHERE <condition>
Select fields
It is also possible to select just some of the fields to be included, by listing the
fieldnames after SELECT.
Ordering data
Records can be sorted into ascending (ASC) or descending (DESC) order:
SELECT <fieldnames>
FROM <table name>
ORDER BY <fieldname> ASC ¦ DESC
Select records
The WHERE clause can be used to specify any conditions that must be met. Only
records that meet those conditions will be displayed.
It is also possible to combine selecting which fields to display and only the records
that meet a criteria.
The Beetle comes before the Focus because descending order of Reg Year was
chosen.
SELECT *
FROM cars
WHERE Colour = ‘Blue’ AND Transmission = ‘M’
Activity – select
1) Examine the countries table and SQL query below:
a) Show which data will be output when the SQL query is run on the table
above.
See who can get the highest score in the select card game from SQL Zoo at
http://tiny.cc/sqlcardgame
Wild cards
In the activity above, you were able to select data where only part of the data in
a field met the criteria. This was done using wild cards. Wild cards replace text in
a string field to represent any character.
SELECT <fieldnames>
FROM <table name>
WHERE <fieldname> LIKE <string with wildcard>
This looks for any values in the Model field that start with A followed by anything
(%).
Similarly, the wild card can be used at the beginning, in the middle or at both the
beginning and end of a string:
Activity – currencies
Using the currencies database, create SQL queries to find:
1) Country names of any country that uses the Euro in alphabetical
order.
2) Names of countries that end in ‘land’.
3) Names of countries and their code that have a code beginning with C.
4) Names of countries that include the word ‘and’.
5) Country names and the currency of countries that use any form of dollar.
6) Country names and regions of countries in any part of Asia.
7) Country names and Income Group of countries that are classed as High
income.
8) Country names and Income Group of countries that have any variation of
middle income.
IN
Your teacher will tell you whether you need to record your answers on the
worksheet or only use SQL Zoo online.
The IN keyword will look for data in a list. The list of values that can be included will
be separated by commas.
SELECT <fieldnames>
FROM <table name>
WHERE <fieldname> IN (<values separated by commas>)
Example – IN
To show all Nissan, Ford and Volkswagen cars:
SELECT Registration, Make, Model
FROM cars
WHERE Make IN (‘Nissan’, ‘Ford’, ‘Volkswagen’)
The IN keyword replaces the need to use lots of OR statements. The equivalent
SQL query using OR instead of IN for the example above would be:
Activity – IN tutorial
Follow through the SQLite tutorial for IN at http://tiny.cc/sqlin
Your teacher will tell you whether you need to record your answers on the
worksheet or only use SQL Zoo online.
BETWEEN
The BETWEEN keyword will find data between two values:
SELECT <fieldnames>
FROM <table name>
WHERE <fieldname> BETWEEN <low value> AND <high value>
SELECT <fieldnames>
FROM <table name>
WHERE <fieldname> >= <low value>
AND <fieldname> <= <high value>
Example – BETWEEN
To show all Nissan, Ford and Volkswagen cars:
SELECT Registration, Make, Model, RegYear
FROM cars
WHERE RegYear BETWEEN 2001 AND 2010
Your teacher will tell you whether you need to record your answers on the
worksheet or only use SQL Zoo online.
Your teacher will tell you whether you need to record your answers on the
worksheet or only use SQL Zoo online.
Your teacher will tell you whether you need to record your answers on the
worksheet or only use SQL Zoo online.
Activity – books
Using the books database, create SQL queries to find:
1) All titles, places and dates of publication for titles published in Derby,
Swindon or Brecon in order of the date of publication with the latest
published book appearing first. Do not use OR as part of the conditional
statement.
2) All titles and dates of publication for titles published between 1980 and 1989.
3) All titles and physical descriptions for titles which include a map (ignore the
genre field, but look at the physical description field).
4) All titles and places of publication for titles which include the Welsh
language.
5) The names of authors who have had books published in Singapore in 2008.
6) All titles and countries of publication of books that include an online
resource.
© paullong.net 2021 Page 25 of 35 by Paul Long
The Ultimate GCSE CS Textbook for AQA – Chapter 7 Published by paullong.net
Multiple tables
SQL queries can find data from more than one table. For AQA GCSE Computer
Science, you will never be asked to find data from more than two tables.
When using two tables, there may be fields that have the same name. Therefore,
each field should be prefixed by the table name:
tablename.fieldname
Data from two tables can be linked by comparing the primary key and foreign
key. If they match, then records will be linked together.
SELECT <fieldnames>
FROM <table name 1>, <table name 2>
WHERE <foreign key> = <primary key>
The courses that an employee attends are stored in a separate table called
Attendance. This table has a primary key of QualificaitonID. A sample of records is
shown below:
QualificationID EmployeeID CourseID DateAttended Pass
1 008 1 22/07/2020 No
2 009 3 21/06/2020 No
3 006 6 17/06/2020 Yes
4 006 2 19/06/2020 Yes
5 006 1 23/06/2020 Yes
The Attendance table has two foreign keys. EmployeeID is a foreign key that links
to the primary key EmployeeNumber in the Employees table. CourseID is a foreign
key that links to the CourseID in the Courses table.
A course can have many attendances. An employee can also have many
attendances. Each attendance only has one course and one employee.
To find the names of the employees who were in attendance, we could use:
SELECT QualificationID, EmployeeID, Forename, Surname
FROM attendance, employee
WHERE EmployeeID = EmployeeNumber
What do you think will happen if the following SQL query is run?
SELECT QualificationID, CourseID, CourseTitle
FROM attendance, course
WHERE CourseID = CourseID
The SQL query did not work. This is because there are two fields called CourseID.
One field is in the attendance table and the other in the course table. The query
for employees above worked because EmployeeID was named differently to
EmployeeNumber.
To solve this problem, the name of the table needs to prefix any fields that exist in
both tables:
SELECT QualificationID, Attendance.CourseID, CourseTitle
FROM attendance, course
WHERE Attendance.CourseID = Course.CourseID
Note: although JOIN methods could be used, at this level it is OK to just find data
by matching the foreign and primary keys.
1) Predict what the output will be for the SQL query below:
2) Correct the SQL query below to find the title, artist ID of all the albums and
the name of the artist featured on each album in alphabetical order of the
album title.
Inserting data
Records can be insert into a table using the INSERT command:
INSERT <table name>
VALUES (<value1>, <value2>, ...)
(<value11>, <value12>, ...)
...
(<value91>, <value92>, ...)
To add multiple records at the same time, separate each record using a comma:
INSERT INTO cars
VALUES ('BD71FZZ', 'Volvo', 'XC90', 'Green', 2021, 'M'),
('KB69CRH', 'Mitsubishi', 'Outlander', 'White', 2019, 'A'),
('EA55XPZ', 'Ford', 'S-Max', 'Silver', 2015, 'A')
From the tutorial above, you will see that it is possible to add data without using all
the fields. To do this, the field names must be specified:
INSERT <table name> (<fieldname1>, <fieldname2>, . . .)
VALUES (<value1>, <value2>, ...)
(<value11>, <value12>, ...)
...
(<value91>, <value92>, ...)
© paullong.net 2021 Page 29 of 35 by Paul Long
The Ultimate GCSE CS Textbook for AQA – Chapter 7 Published by paullong.net
Note: MS Access only allows one record to be inserted at a time using SQL.
1) a) Adapt the SQL query below to insert a new employee called Melissa
Stokes who is a member of the IT staff reporting to Michael Mitchell.
No other information is known about her yet.
INSERT INTO employees (LastName, FirstName, Title, ReportsTo)
VALUES ('Edwards', 'Nancy', 'SalesManager', 1)
b) A new artist called Jessie Hackett who will have an ArtistID of 276. You
should not include fieldnames after INTO.
d) A new invoice item (ID 2241) for the sale of one track of Momentos Que
Marcam on invoice 412 for 79p. You will need to find out some of
the data needed from the tracks table. Click on the SQL link or
open the test query in MS Access to see if it has worked.
© paullong.net 2021 Page 30 of 35 by Paul Long
The Ultimate GCSE CS Textbook for AQA – Chapter 7 Published by paullong.net
Updating data
Data values within a table can be updated. The table name should be specified
and then the new values for each field. Finally, the record(s) which should be
changed should be identified using the WHERE statement.
UPDATE <tablename>
SET <fieldname1> = value1, <fieldname2> = value2
WHERE <condition>
Run the SQL query to update the table and watch what happens:
UPDATE cars
SET Colour = 'Pink'
WHERE Registration = 'BD07ABC'
Predict the effect of the following update query on more than one record:
UPDATE cars
SET Make = 'Nissan UK'
WHERE Make = 'Nissan'
Predict the effect of the following update query on more than one field:
UPDATE cars
SET Make = 'BMW', Model = 'Mini Cooper'
WHERE Model = 'Cooper'
© paullong.net 2021 Page 31 of 35 by Paul Long
The Ultimate GCSE CS Textbook for AQA – Chapter 7 Published by paullong.net
UPDATE employees
SET LastName = 'Matthews'
WHERE EmployeeID = 2
a) Kathy Chase, who is a customer, has married and changed her surname
to Bartholomew.
b) The price of all tracks that cost £1.99 have been reduced to £1.64.
d) The track called Princess of the Dawn has changed its media type to be
an MPEG audio file. No other tracks have changed their media type.
Deleting data
Records can be deleted using the DELETE command. To do this, the table needs
to be specified and the records selected for deletion using WHERE.
Run the SQLquery and the whole record for that car will be deleted:
Registration Make Model Colour Reg Year Transmission
BX03HMW Ford Focus Blue 2003 M
BR54URS Vauxhall Astra Red 2004 M
BA55WEP Volkswagon Beetle Blue 2005 M
BC13PRS Mini Cooper Green 2013 A
BD07ABC Nissan Almera Red 2007 M
BE14RTJ Nissan Leaf Blue 2014 A
Questions – follow me
1) Examine the database tables for students and classes below:
b) Write an SQL query to show only the names of all male students in 10TR.[3]
c) The capacity (maximum number of students) for each class in tiers 3 and
4 needs to be reduced by 2. Refine the SQL query below to correct the
errors.
UPDATE students
SET tier = tier - 2
WHERE tier BETWEEN (3,4) [3]
d) Write an SQL query to show the names of all students and their classroom
who have a classroom that starts with the letter B. [4]