0% found this document useful (0 votes)
2 views

1.3.1 Databases

This document outlines the learning objectives and key concepts related to databases in computer science, including types of databases, entity relationships, and database design. It covers the structure of databases, primary and foreign keys, and the importance of referential integrity. Additionally, it provides examples of entities and attributes relevant to a subscription-based system, along with homework assignments for exam preparation.

Uploaded by

Roksi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

1.3.1 Databases

This document outlines the learning objectives and key concepts related to databases in computer science, including types of databases, entity relationships, and database design. It covers the structure of databases, primary and foreign keys, and the importance of referential integrity. Additionally, it provides examples of entities and attributes relevant to a subscription-based system, along with homework assignments for exam preparation.

Uploaded by

Roksi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

OCR Computer Science

Comp 1 – TOPIC
Computer Systems

Parvinder Kaur
Recap according to A level spec
Learning Objectives
In this lesson, you will be learning about the
Databases. You will cover:
1. Understand the structures of Databases
2. Show an understanding by using real life
scenarios
3. Able to link tables together
H406 01 - Spec 1.3.1
Databases

Databases

• Relational database, flat file,


primary key, foreign key,
secondary key, entity
relationship modelling
• Methods of capturing,
selecting, managing and
exchanging data.
A simple database

• The simplest kind of database is a flat file,


consisting of information about a single entity
• An entity is a category of object, person, event
or thing of interest about which data needs to
be recorded
• For example you might hold data about club
members or concert venues
Database design

• Most databases hold data about several


entities
• Suppose you are going to design a new system
for a company selling subscriptions for online
revision guides
• What entities are there in a system that will
keep records of subscriptions for revision
guides?
Entities
• You may have thought of these entities:
– Customer
– Guide or Product
– Subscription
• Other entities that could be considered include
customer order, subject, author (of a revision guide)
• We will keep it simple and just consider Customer,
Product and Subscription
• What data would you keep about each of these
entities?
Attributes
• This will be a database system

• Each entity in the database has attributes


• The entity descriptions can be written in this
format:
– Customer (custID, title, firstname, surname, email)
– Product (productID, title, subject, level, price)
– Subscription (subID, startDate, endDate)
Primary Key
• Each entity needs an identifier which uniquely
identifies a particular record
• In a relational database, the identifier is known as
the primary key
• It is underlined in the entity description:
– Customer (custID, title, firstname, surname, email)
– Product (productID, title, subject, level, price)
– Subscription (subID, startDate, endDate)
• If there is no natural attribute for a primary key, one
should be introduced
Composite key
• Sometimes two or even more attributes are needed
to uniquely define a record
• For example, in a customer order consisting of
many different order lines, each order line may be
uniquely identified by the two attributes
orderNumber and orderLine
• OrderLine (OrderNumber, OrderLine, ProductID, …)
• OrderNumber, OrderLine is a composite primary
key
Entity Relationship Diagram
• The three entities are linked, or related
• There are three possible ways in which two
entities may be related:
– One-to-one e.g. Husband and Wife
– One-to-many e.g. Mother and Child, School and Pupil
– Many-to-many e.g. Actor and Film, Recipe and
Ingredient
The diagram
• An entity relationship (E-R) diagram is a
graphical way of representing the
relationships between entities
One-to-One

One-to-Many

Many-to-Many

• We can say, for example, that one school has


many pupils, or many pupils attend one school
Database Structure
• Each entity is represented by a table
• Tables in a relational database are commonly
referred to as relations
• A database contains one or more relations
• A relation has rows, each row containing one
record
• The columns in the relation each contain one
field (i.e. attribute) belonging to the records
Creating a relationship
• To create a relationship between Customer and
Subscription, we need to include custID in the entity
description of Subscription
– Subscription (subID, startDate, endDate, custID )
• ProductId also needs to be included in the entity
description of Subscription
– Subscription (subID, startDate, endDate, custID, productID)
• custID and productID are foreign keys in Subscription,
shown in italics
– A foreign key always goes on the “many” side of a
relationship
Foreign key: definition

• A foreign key is an attribute that creates a join


between two tables (relations)
• It is the primary key in the first relation

• Draw an entity relationship diagram to show the


relationships between the three entities
Referential integrity

• Referential integrity means that no foreign key


in one table can reference a non-existent
record in a related table
• For example, it should not be possible to add a
subscription for a customer with custID C100 if
there is no record for customer C100
The RevisionSubs database

• The tables (relations) represent the three


entities described, each with its own
attributes
• We will use the convention of giving each
table name the prefix tbl
– Each table contains some sample data
RevisionSubs tables

tblCustomer custID title firstname surname email


C111 Mr Fred Carr [email protected]
C245 Miss Mabel Jenkins [email protected]
C364 Miss Jasmine Kumar [email protected]

tblSubscription subID startdate endDate custID productID


S1211 25/02/2016 24/02/2017 C111 P36
S1212 01/02/2016 31/01/2017 C111 P47
S1213 04/02/2016 03/02/2017 C245 P36

tblProduct productID productName subject level price


P24 Equations Maths 2 £12.00
P36 Programming Comp Science 4 £25.00
P47 Database Comp Science 4 £25.00
Many-to-many relationships

• When there is a many-to-many relationship


between tables, they cannot be directly linked
– For example, you cannot link the entities
Customer and Product directly

• Why not?
Many-to-many relationships

• You would need several fields in tblCustomer


to hold the ProductID of each product a
customer has subscribed to
• But how many fields would you allow?
– How would you find all customers who had
subscribed to a particular product?
custID title firstname surname email ProdID1 ProdID2
C111 Mr Fred Carr [email protected] P36 P47
C245 Miss Mabel Jenkins [email protected] P36
C364 Miss Jasmine Kumar [email protected] P25 P36
Many-to-many relationships

• This configuration does not work


• It is impractical to allow several fields ProdID1,
ProdID2, etc.
• You cannot easily extract information from this
table
• An alternative way of organising the data is
required
custID title firstname surname email ProdID1 ProdID2
C111 Mr Fred Carr [email protected] P36 P47
C245 Miss Mabel Jenkins [email protected] P36
C364 Miss Jasmine Kumar [email protected] P25 P36
Linking tables
• Suppose you have a table holding details of gym
members and the classes they take – yoga,
indoor cycling, pilates, interval training, etc.

• You need a link table “in the middle” just as the


entity Subscription was between Customer and
Product
Recap according to A level spec
Exam Preparation for Thursday and
HOMEWORK
OCR Book

1. Make notes from the Powerpoint slides on VLE


2. Make notes on Pages 82 - 89
3. Answer questions on pages 86-87
4. Extra revision. Go through the PPT in the folder along with
the tasks

ALL incomplete work MUST be done for homework ready for


THURSDAYS MOCK

You might also like