OCR A Level Computer Science: 3.2 Databases
OCR A Level Computer Science: 3.2 Databases
3.2 Databases
Contents
Relational Databases
Data Normalisation
Entity Relationship Diagrams
Capturing, Selecting, Managing & Exchanging Data
SQL
Referential Integrity
Transaction Processing
Page 1 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Relational Databases
Your notes
Relational Databases
What is a Database?
A Database is an organised collection of data
It allows easy storage, retrieval, and management of information
Electronic databases offer a number of key benefits:
Easier to add, delete, modify and update data
Data can be backed up and copied easier
Multiple users, from multiple locations, can access the same database at the same time
Database Terminology
Term Definition
Primary key A unique identifier for each record in a table. Usually an ID number
Compound primary key A combination of (2 or more) fields that is unique for all records
(sometimes called
composite)
Foreign key A field in a table that refers to the primary key in another table. Used to link
tables and create relationships
Secondary key A field or fields that are indexed for faster searching
Page 2 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Database Management Software used to manage databases. Examples include MySQL, Oracle,
System Microsoft SQL Server, PostgreSQL
Your notes
(DBMS)
Page 3 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 4 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
This table has redundant data - the tutor and form room information repeats
This is inefficient
If a tutor changed their name we would need to find all instances of that name and change them all
Missing any would mean the table had inconsistent data
A relational database would solve this issue:
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
Page 5 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Now the name of each tutor and their form room is stored only once
This means if they change only one piece of data, the data is updated in the entire database and
Inconsistency is avoided
WORKED EXAMPLE
Customers’ details are stored in the flat file database table Customer. An extract of the table is
shown below
Page 6 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Describe one problem that would arise with the flat file database structure if a customer wanted
to insure more than one car at the same time
[2]
Answer:
One mark per pair:
Only one customer entry allowed (because of key field)...
so would not be able to add second entry [1]
Customer data already present/would be repeated...
resulting in redundant data/wasted space/inconsistencies should changes be made [1]
Page 7 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Data Normalisation
Your notes
First Normal Form (1NF)
What is Normalisation?
Normalisation is the process of organising a database to reduce data duplication and improve data
accuracy and consistency
Achieved by applying a set of guidelines (forms), each with specific rules and requirements
Enhances database efficiency and maintainability
Provides consistency within the database
Page 8 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
This customers table below has a primary key and the name is stored in two fields so it is atomic
This table is in first normal form
Page 9 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
All non-prime attributes (attributes not part of the primary key) must be fully dependent on the
primary key
Have no partial dependencies Your notes
Non-prime attributes must not depend on only part of the primary key (in case of a composite
primary key)
Separate tables should be created for partially dependent attributes
In this table below, Course Title only depends on part of the compound primary key (the course code) and
not the Date so this table is not in second normal form
Page 10 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
EXAM TIP
For a table to be in second normal form it has to be in first normal form.
For a table to be in third normal form it has to be in second normal form.
So if asked for the rules of either second or third normal form make sure you say this.
WORKED EXAMPLE
An airport holds details of flights in a database using the table Flight. An extract of the table is
shown below.
Page 11 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
iii) Describe why the database can not be considered to be in Third Normal form
[2] Your notes
Answer:
No Repeating fields/data. Data is atomic. Has a primary Key [2]
Is in First Normal Form. Every field is dependent on the primary key [2]
Has a transitive relationship. A non-key field depends on another non-key field. DestinationName
depends on DestinationCode [2]
Page 12 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 13 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
EXAM TIP
These diagrams are simple but tell us some important things about the database:
The names of all the tables
Which tables will have a foreign key - when an entity has a ‘many’ relationship against it that
means it will have a foreign key in it that links to the primary key of the connected entity
WORKED EXAMPLE
An insurance company's offices have a large number of black and white printers
The company's technicians keep accurate records of the printers in the building, and the quantity
of toner cartridges in stock, in a flat file database. An extract of the database is shown:
Page 14 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 15 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 16 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Forms
Collect user input
Organise data in structured format
Common in web applications
Page 17 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Page 18 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Sensors
Devices that detect and respond to changes in environment
Convert physical signals into digital data
Facilitates automated data collection and real-time monitoring
Page 19 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 20 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 21 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Involves techniques and algorithms from fields like machine learning, statistics, and artificial
intelligence
Your notes
Supports decision-making by transforming raw data into valuable information
Applications of data mining
Marketing: Customer segmentation, market basket analysis, and targeted advertising
Finance: Fraud detection, credit scoring, and portfolio management
Healthcare: Disease prediction, patient clustering, and drug discovery
Manufacturing: Quality control, predictive maintenance, and supply chain optimisation
Telecommunications: Network monitoring, customer analysis, and service improvement
Selecting Data
Query By Example (QBE)
User-friendly method for constructing database queries using a visual interface
Allows users to search for data by providing an example of the desired output
Key Features
Visual representation: QBE uses a grid or form-based interface, where users can enter criteria in
columns representing database fields
Intuitive: Users don't need to learn complex query syntax, making it accessible for non-technical
users
Flexible: Allows for simple to complex queries, including filtering, sorting, and joining multiple
tables
How QBE Works
Users enter criteria in the appropriate columns or fields in the QBE grid or form
The QBE system translates the user's input into an equivalent SQL query or other query language
The query is executed against the database, and the results are displayed to the user
Common Query Operations
Filtering: Users can specify conditions or criteria in the QBE grid to retrieve specific records (e.g., all
customers from a particular city)
Page 22 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Sorting: Users can indicate the desired sorting order for the results (e.g., ascending or descending by
last name)
Your notes
Joining: Users can combine data from multiple related tables by specifying relationships between the
tables in the QBE grid
Aggregation: Users can perform calculations or summaries on the data (e.g., counting the number of
records, calculating averages, or summing values)
Benefits and Drawbacks of Query By Example (QBE)
Drawbacks
Benefits
Easy to learn and use, even for non-technical users Less powerful and flexible than SQL for complex
queries and data manipulation
Visual interface makes it simple to understand and May not support advanced database features,
modify queries such as stored procedures or triggers
Provides a more accessible way to perform database Can be slower than SQL queries for large
searches compared to writing SQL queries datasets or complex operations
Exchanging Data
Page 23 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 24 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Memory Sticks
Portable storage devices, also known as USB flash drives or thumb drives
Use flash memory to store and transfer data between computers and other devices
Connect to devices via USB (Universal Serial Bus) port
Benefits and Drawbacks of Memory Sticks
Benefits Drawbacks
Easy to use: Plug-and-play functionality with no need for Limited storage capacity compared to
additional software external hard drives
Portable: Compact size allows for easy transport and Data loss risk due to physical damage, loss,
storage or theft
Durable: No moving parts, making them resistant to Slower transfer speeds compared to other
physical damage storage solutions
Page 25 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Email
Electronic communication system used for exchanging messages and files between users
Requires internet access and an email account with an email service provider
Benefits and Drawbacks of Email
Benefits Drawbacks
Fast: Allows for near-instant communication Limited file size: Most email service providers impose
across the world restrictions on attachment sizes
Convenient: Accessible on various devices, Security risks: Vulnerable to phishing attacks, spam,
including computers, smartphones, and and data breaches
tablets
Organised: Stores and organises messages in Privacy concerns: Email messages and attachments
folders, such as Inbox, Sent, and Drafts can be intercepted, read, or modified by unauthorised
parties
Versatile: Supports the attachment of various Reliability: Delivery issues can occur due to server
file types and sizes problems, incorrect email addresses, or spam filters
Page 26 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
SQL
Your notes
Selecting Data (Single Table)
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:
Select data (single table)
Select data (multiple tables)
Insert data
Delete records
Delete tables
FROM Specifies the tables to retrieve data from SELECT name, age FROM users;
(retrieves names and ages from the
'users' table)
WHERE Filters the data based on a specified condition SELECT * FROM users
WHERE age > 30;
Page 27 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
LIKE Filters the data based on a specific pattern SELECT * FROM users
WHERE name LIKE 'J%';
(retrieves users whose names start
with 'J')
WILDCARDS '*' and '%' symbols are used for searching and SELECT * FROM users;
matching data (retrieves all columns for the 'users'
'*' used to select all columns in a table table)
'%' used as a wildcard character in the LIKE
operator SELECT * FROM users WHERE name
LIKE 'J%';
(retrieves users whose names start
with 'J')
Examples
Select all the fields from the Customers table
Command:
Output:
Page 28 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Select the ID, name & age of customers who are older than 25
Command:
Output:
ID Name Age
1 John Doe 30
3 Peter Lee 40
Select the name and country of customers who are from a country that begins with 'U'
Command:
Output:
Name Country
Page 29 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Output:
WORKED EXAMPLE
Customers’ details are stored in the flat file database table Customer. An extract of the table is
shown below
Write the SQL statement that would show only the CustomerID and Surname fields for customers
with the Title “Miss” or “Mrs”
[4]
Page 30 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Answer:
SELECT CustomerID, Surname [1] Your notes
FROM Customer [1]
WHERE Title="Miss" [1]
OR Title = "Mrs" [1]
Nested A select within another select statement SELECT * FROM users WHERE age >
SELECT (nested). A mini select within the main one (SELECT AVG(age) FROM users);
(retrieves users with an age greater than
the average age)
JOIN Combines data from two or more tables SELECT users.name, orders.order_id
(INNER based on a related column FROM users
JOIN) JOIN orders
ON users.user_id = orders.user_id;
(retrieves user names and their
corresponding order IDs)
Examples
Table: Employees
ID Name Salary Department City
Select all fields for employees whose salary is bigger than the average salary of all employees
Page 31 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Command:
Your notes
Output:
Inserting Data
Example
Command Description
INSERT Adds new data to a database INSERT INTO users (name, age)
table VALUES ('John Doe',25);
(inserts a new user with the name 'John Doe' and age
25)
Example
Table: Employees
Insert a new employee into the Employees table with the 'Name', 'Salary', 'Department' and 'City' fields
Page 32 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Command:
Your notes
Output:
Deleting Records
Example
Command Description
Example
Table: Employees
Page 33 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Delete all records from the Employees table whose department is 'Marketing'
Command:
Output:
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.
Page 34 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Write an SQL statement that will remove all songs by RandomBits from the table Song
Your notes
[2]
Answer:
DELETE FROM Song [1]
WHERE Artist = “RandomBits” [1]
Deleting Tables
Example
Command Description
Example
Table: Employees
Page 35 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Output
Your notes
Page 36 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Referential Integrity
Your notes
Referential Integrity
What is Referential Integrity?
Ensures consistency between related tables in a relational database
Maintains valid relationships between primary and foreign keys
There should not be foreign keys for which a matching primary key in the linked table does not exist
Foreign key constraints
Value in a foreign key field must either:
Match a primary key value in the related table, or
Be null (if allowed)
Enforce referential integrity
Rules:
Cascade actions
CASCADE: automatically makes changes to related records
SET NULL: sets foreign key value to null in related records
SET DEFAULT: sets foreign key value to its default in related records
NO ACTION/RESTRICT: prevents changes if related records exist
Update or delete actions will take effect everywhere in the database automatically
Types:
Benefits and Drawbacks of Referential Integrity
Drawbacks
Benefits
Ensures data consistency and accuracy Can impact performance due to additional checks
Page 37 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
WORKED EXAMPLE
A hotel uses a computer system to keep track of room bookings. The hotel staff are able to query
a database to discover which rooms are booked or which rooms are free
The hotel booking database enforces referential integrity.
Explain what is meant by the term ‘referential integrity’ and how this could potentially be broken
[2]
Answer:
Database/relationships are consistent and each foreign key links to an existing/valid primary key [1]
If a primary key is deleted, foreign keys that link to it are no longer valid so they should also be
deleted - this is known as a cascaded delete [1]
Page 38 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Transaction Processing
Your notes
Transaction Processing
What is a Transaction?
A transaction is a sequence of database operations treated as a single unit of work
Ensures data consistency and integrity during simultaneous access
Example: money transfer between bank accounts. The transaction here would be a withdrawal from
one account and a deposit into another. Both operations must happen together or neither should
happen
Problems that arise from transaction processing
Concurrency: When multiple transactions are executed simultaneously, they might try to access
or modify the same data, leading to inconsistencies
Deadlock: This occurs when two or more transactions are waiting for each other to release
resources, causing them to wait indefinitely
Data Integrity: Transactions might leave the database in an inconsistent state if they fail in the
middle of execution
Isolation: In a multi-user environment, one user's transaction might affect another user's
transaction, leading to unpredictable outcomes
Durability: If a system fails after a transaction has been confirmed, it may result in loss of data
Solutions to overcome problems that arise in transaction processing
Locking: Implementing a locking mechanism ensures that no two transactions can access the
same data simultaneously. There are two types of locks: shared locks and exclusive locks
Deadlock Prevention or Deadlock Detection: Deadlocks can be prevented by ordering the way in
which resources are requested or by having a timeout mechanism
Logging and Recovery: By keeping a log of all changes, the system can recover to a consistent
state after a crash
Commit and Rollback: The commit operation saves all changes made in the transaction as
permanent. The rollback operation reverts the changes made in the transaction
Page 39 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Two-Phase Commit Protocol: In distributed systems, this protocol ensures that a transaction is
committed in all participating nodes, or none at all, to maintain consistency
Your notes
Concurrency control
Manages simultaneous access to data in a multi-user environment
Techniques:
Locking: prevents multiple transactions from accessing the same data simultaneously
Timestamping: assigns a unique timestamp to each transaction
Transaction management
Use database management systems (DBMS) with built-in transaction support
Implement custom transaction logic using SQL or other query languages
Test and monitor transactions to ensure ACID compliance and performance
ACID
ACID (Atomicity, Consistency, Isolation, Durability)
A set of rules that all Database Management Systems (DBMS) must use to ensure data integrity
Guarantee reliable processing of transactions
Atomicity
All operations in a transaction succeed or fail as a whole
If any operation fails, the transaction is rolled back
Ensures partial transactions do not occur
Consistency
Ensures data remains in a consistent state after transactions
Transactions must follow database rules and constraints
Starts with a consistent state and ends with a consistent state
Isolation
Transactions are isolated from each other
Page 40 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
WORKED EXAMPLE
RestaurantReview is a website that allows users to leave reviews and ratings for different
restaurants
The website uses a database with the following structure
Record Locking
Record locking is a technique used in database management systems (DBMS) to prevent conflicting
access to data by multiple transactions or processes
It ensures data consistency and integrity when multiple users or processes try to read, modify, or
delete records simultaneously
Key Concepts
Page 41 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Lock: A mechanism that prevents access to a database record by other transactions while a
specific transaction is using the record
Your notes
Lock granularity: Refers to the size of the locked data, ranging from a single row to an entire table
Types of Locks
Shared lock (Read lock): Allows multiple transactions to read a record simultaneously, but prevents
modifications or deletions until the lock is released
Exclusive lock (Write lock): Allows only one transaction to access and modify a record, blocking
other transactions from reading or writing to the locked record until the lock is released
Benefits of Record Locking
Maintains data consistency and integrity by preventing conflicting modifications
Allows concurrent read access to records without compromising data consistency
Improves database performance by enabling multiple users to access data simultaneously, while
managing access to prevent conflicts
Redundancy
Redundancy can occur when the same piece of data is stored in more than one table in a database.
This can either be by accident or by design
It can lead to inconsistencies in the data and/or wasted storage space
Page 42 of 42
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers