Revision Notes AQA-Computer Science Prime Tuition
Relational Databases
What is a database?
A Database is a structured, persistent collection of data
It allows easy storage, retrieval, and management of
information
Electronic databases offer a number of key benefits:
o Easier to add, delete, modify and update data
o Data can be backed up and copied easier
o Multiple users, from multiple locations, can access the same
database at the same time
There are two main types of database
o Flat file
o Relational
Flat file database
A flat file database is one that stores all data in a single table
It is simple and easy to understand but causes
data redundancy, inefficient storage and is harder to maintain
Consider this example flat file table of students
o This table has redundant data - the tutor and form room
information repeats, this is inefficient
Revision Notes AQA-Computer Science Prime Tuition
o If a tutor changed their name we would need to find all
instances of that name and change them all
o Missing any would mean the table had inconsistent data
Relational database
A relational database is one that organises data into multiple
tables
It uses keys to connect related data which reduces data
redundancy, makes efficient use of storage and is easier to
maintain
A relational database solves the issues in a flat file database:
o A new table could be created to store the tutor information
and the tutor information in the student table could be moved
to the new table. Then a foreign key in the
student table (TutorID) could link a student to their tutor
Revision Notes AQA-Computer Science Prime Tuition
o Now the name of each tutor and their form room is stored only
once
o This means if they change only one piece of data, the data is
updated in the entire database and Inconsistency is
avoided
Tables & Records
What is a table?
A table is a complete set of records about the same subject/topic
in a database
cars
An example of a database table named cars
What is a record?
A record is complete set of fields on a single entity in a table
(row)
cars
An example of a record in the cars table
Fields & Data Types
What is a field?
A field is a single piece of data in a table (column)
cars
Revision Notes AQA-Computer Science Prime Tuition
An example of a field in the cars table
What is a data type?
A data type is the type of data that can be held in a field and is
defined when designing a table
Examples of common datatypes are:
o Numeric - whole/decimal
o Character/string - text data
o Date/Time
o Boolean - true or false values
In the car table above, the following datatypes would be used:
o car_id: numeric
o make: string
o model: string
o colour: string
o price: numeric
Primary Keys & Foreign Keys
What is a primary key?
A primary key is a unique field that can be used to identify a
record in a table
order_id is the primary key for the orders table
Revision Notes AQA-Computer Science Prime Tuition
customer_id is the primary key for the customers table
What is a foreign key?
A foreign key is a field in a table that refers to the primary key in
another table.
A foreign key is used to link tables and create relationships
In the orders table customer_id is a foreign key - it links an order
back to the customer that made the order in the customer table
Key database terminology
Term Definition
Table A collection of records with a similar structure
Record A group of related fields, representing one data entry
Field A single piece of data in a record
Data
Type of data held in a field
type
Primary
A unique identifier for each record in a table. Usually an ID number
key
Revision Notes AQA-Computer Science Prime Tuition
Foreign A field in a table that refers to the primary key in another table. Used to link
key tables and create relationships
Worked Example
A relational database has been developed for a dance club to store
information about their members and the styles of dance they practice.
The database contains two tables: Members and Styles
Figure A shows some data from the tables.
Members
MemberID FirstName LastName DateJoined
1 Zarmeen Hussain 2024-01-19
2 Fyn Ball 2024-02-01
3 George Johnson 2024-02-25
4 Ella Franks 2024-03-04
Styles
StyleID MemberID Style DateStarted
1 1 Hip Hop 2024-01-22
2 1 Ballroom 2024-02-13
3 3 Contemporary 2024-03-01
4 2 Street 2024-03-07
(a) State one benefit of using relational databases [1]
(b) State the name of the field from the Members table that is the most
suitable to use as the primary key [1]
(c) State the name of the field from the Styles table that is a foreign
key [1
Answers
(a) reduces data redundancy // reduces data inconsistency
(b) MemberID
(c) MemberID
Guidance
Revision Notes AQA-Computer Science Prime Tuition
Ignore case (b, c)
No mark if inside quotation marks (b, c)
No mark if obvious space in response (b, c)
What is SQL?
SQL (Structured Query Language) is a programming language used
to interact with a DBMS.
The use of SQL allows a user to:
o Select data (flat file)
o Select data (relational)
o Order data
o Insert data
o Update data
o Delete records
Selecting data commands (flat file)
Comman
Description Example
d
SELECT * FROM users;
(retrieves all data from the 'users' table)
Retrieves data from a
SELECT SELECT name, age
database table
FROM users
(retrieves names and ages from the 'users' table)
Specifies the tables to SELECT name, age FROM users;
FROM
retrieve data from (retrieves names and ages from the 'users' table)
SELECT * FROM users
Filters the data based on
WHERE WHERE age > 30;
a specified condition
(Retrieves users older than 30)
Combines multiple SELECT * FROM users
AND conditions in WHERE age > 18 AND city = 'New York';
a WHERE clause (retrieves users older than 18 and from New York)
OR Retrieves data when at SELECT * FROM users
least one of the WHERE age < 18 OR city = 'New York';
Revision Notes AQA-Computer Science Prime Tuition
conditions is true (retrieves users younger than 18 or from New York
'*' and '%' symbols are
used for searching and
matching data SELECT * FROM users;
WILDCAR '*' used to select all (retrieves all columns for the 'users' table)
DS columns in a table SELECT * FROM users WHERE name LIKE 'J%';
'%' used as a wildcard (retrieves users whose names start with 'J')
character in the LIKE
operator
A select within another SELECT * FROM users WHERE age >
Nested select statement (SELECT AVG(age) FROM users);
SELECT (nested). A mini select
within the main one (retrieves users with an age greater than the avera
SELECT Forename, Lastname FROM Students
WHERE StudentID < 10
How data is organised ORDER BY Lastname, Forename ASC
ORDER
(sorted) when it is (retrieves only the forename and lastname of all s
BY
retrieved from the students table who have a studentID of l
10 and displays in ascending order
by lastname and forename)
Examples
Select all the fields from the Customers table
Command:
Output:
Select the ID, name & age of customers who are older than 25
Command:
Revision Notes AQA-Computer Science Prime Tuition
Output:
Select the name and country of customers who are from a country
that begins with 'U'
Command:
Output:
Select all fields of customers who are from 'London' or 'Paris'
Command:
Output:
Nested select
Table: Employees
Revision Notes AQA-Computer Science Prime Tuition
ID Name Salary Department City
1 Fynn Roberts 45000 HR London
2 Zarmeen Azra 52000 Sales Manchester
3 Ella Stanley 39500 Marketing Birmingham
Select all fields for employees whose salary is bigger than the
average salary of all employees
Command:
Output:
ID Name Salary Department City
2 Zarmeen Azra 52000 Sales Manchester
Selecting data commands (relational)
Comman
Description Example
d
SELECT users.name,
orders.order_id FROM users
JOIN
Combines data from two or more tables INNER JOIN orders
(INNER
based on a related column ON users.user_id = orders.use
JOIN)
(retrieves user names and the
corresponding order IDs)
Example
Table: Employees
EmployID Name Salary City
1 Fynn Roberts 45000 London
2 Zarmeen Azra 52000 Manchester
3 Ella Stanley 39500 Birmingham
Table: Departments
DepartID EmployID Department Manager Email
1 1 HR Sally Jones hr@myemail.com
Revision Notes AQA-Computer Science Prime Tuition
2 2 Sales Peter Evans sales@myemail.com
3 3 Marketing Stuart Davies marketing@myemail.com
Select the name, manager and email address of employees in the
sales department
Command:
Output:
Inserting Data Using SQL
Comman
Description Example
d
INSERT INTO users (name, age)
Adds new data to a database VALUES ('John Doe',25);
INSERT
table (inserts a new user with the name 'John Doe
25)
UPDATE users
SET name = 'Bob', age = 56
UPDATE Edit data in a database table
WHERE ID = 4;
(updates name and age details for user ID =
Example
Table: Employees
Insert a new employee into the Employees table with the 'Name',
'Salary', 'Department' and 'City' fields
Command:
Revision Notes AQA-Computer Science Prime Tuition
Output:
Table: Employees
Update employee ID 3 to a salary of 47500 and city to London
Command:
Output
Deleting Data Using SQL
Comman
Description Example
d
DELETE Removes data from a database DELETE FROM users
table WHERE age < 18;
(deletes all users younger than 18 from th
table)
DELETE FROM users
WHERE name="John";
Revision Notes AQA-Computer Science Prime Tuition
(deletes a record where the name is John)
Example
Table: Employees
ID Name Salary Department City
1 Fynn Roberts 45000 HR London
2 Zarmeen Azra 52000 Sales Manchester
3 Ella Stanley 39500 Marketing Birmingham
4 George Rope 47250 Sales Leeds
Delete all records from the Employees table whose department is
'Marketing'
Command:
DELETE FROM Employees
WHERE Department = 'Marketing' ;
Output:
ID Name Salary Department City
1 Fynn Roberts 45000 HR London
2 Zarmeen Azra 52000 Sales Manchester
3 George Rope 47250 Sales Leeds
Worked Example
A database stores information about songs on a music streaming service.
One of the tables called Song has the fields Title, Artist, Genre and Length
A band called RandomBits removes their permission for their songs to be
streamed. The company removes all the songs belonging to RandomBits
from their service.
Write an SQL statement that will remove all songs by RandomBits from
the table Song [2]
Answer
DELETE FROM Song [1]
WHERE Artist = “RandomBits” [1]