11.1 Relational Databases
11.1 Relational Databases
Relational Databases
What is a database?
o Flat file
o Relational
A flat file database is one that stores all data in a single table
o This table has redundant data - the tutor and form room
information repeats, this is inefficient
Revision Notes AQA-Computer Science Prime Tuition
Relational database
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
What is a table?
cars
What is a record?
cars
What is a field?
cars
Revision Notes AQA-Computer Science Prime Tuition
A data type is the type of data that can be held in a field and is
defined when designing a table
o Numeric - whole/decimal
o Date/Time
o car_id: numeric
o make: string
o model: string
o colour: string
o price: numeric
Term Definition
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
Members
Styles
2 1 Ballroom 2024-02-13
3 3 Contemporary 2024-03-01
4 2 Street 2024-03-07
(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
(b) MemberID
(c) MemberID
Guidance
Revision Notes AQA-Computer Science Prime Tuition
What is SQL?
o Order data
o Insert data
o Update data
o Delete records
Comman
Description Example
d
Examples
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:
Command:
Output:
Nested select
Table: Employees
Revision Notes AQA-Computer Science Prime Tuition
Select all fields for employees whose salary is bigger than the
average salary of all employees
Command:
Output:
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
Table: Departments
Command:
Output:
Comman
Description Example
d
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
Command:
Output
Comman
Description Example
d
Example
Table: Employees
Command:
Output:
Worked Example
Write an SQL statement that will remove all songs by RandomBits from
the table Song [2]
Answer