A2 Computing
LessonDatabases - Introduction
Aims and Objectives
• To understand what a relational database is• To understand how to design a relational
database using ER modelling• To be able to construct a model of a new
database using this technique
What is a database?
• A structured collection of data
• Data is usually stored in tables
Surname: James
First name: Samantha
Date of Birth: 20.12.89
Address: 16 Poppy Close
Town: Southam
County: Warwickshire
Surname: Brown
First name: David
Date of Birth: 20.12.89
Address: 16 Poppy Close
Town: Southam
County: Warwickshire
Surname: Lastler
First name: David
Date of Birth: 20.12.89
Address: 16 Poppy Close
Town: Southam
County: Warwickshire
Surname: Whitefield
First name: Nina
Date of Birth: 21.02.76
Address: 16 Poppy Close
Town: Southam
County: Warwickshire
Flat Files A flat file is a database held in a single file consisting of one table.
Video no.
Title Country
Date release
d
Classification
Rental price
1 Lord of the Rings US 2001 12 3.50
2 Attack of the Clones
US 2002 PG 2.99
3 About a boy UK 2002 U 2.99
4 Crocodile Dundee AUS 1986 15 1.50
5 Die another day UK 2002 12 2.99
6 Shrek US 2001 U 1.99Record
Field File
Flat File problems
• There are a number of problems using flat files where everything is stored in one table– data is often repeated in several different places– as a result, updating of data is time consuming
and often inaccurate– data entry is time consuming and prone to errors
Using multiple tables
• Using a database with multiple tables (a relational database) means that data can be separated into a more structured design
• Data between tables is linked so that it is accessed only when required ( a relationship is established)
• Advantages– Reduces the repeating of data (redundant data)– Reduces error on updating and inputting of data– Increases the reliability of the database (data integrity)– Increases speed of data retrieval
Building a database
• Before a relational database can be built it needs to be designed so it works effectively and efficiently
• The data requirements are carefully analysed • A data model is built
– a method of describing the data, the structure and how it is interrelated, the constraints that apply
• You will be learning the process………
Entity-relationship modelling•An entity is:
–An object, person or a thing of interest represented in a database.
• Customers, Employees, Products, Orders.
•Each entity will normally be represented by a table in the relational database.
•Entity names are normally in the singular–Customer not Customers
• An attribute is:– The information held about an entity.– e.g. Customer attributes:
» Surname» Forname» Address» Phone number
• Attributes are normally stored in fields.
Entity-relationship modelling
Question - Entity or Attribute?
• Car model• Car• Colour• Registration number• Date of Registration• Cost
• Attribute• Entity• Attribute• Attribute• Attribute• Attribute
Car Table
Model Colour Date of Registration
Registration number
Cost
Passat Blue 31/01/06 AZ06 GNS £12,000
Golf Silver 01/08/05 AS56 CHA £11,500
Question
• A database is to store details of the following – students at college– A level courses
• Write down suitable entity names for these• For each entity write down possible attributes• Draw the database table that would represent
these details
Entity relationships
• The link between two entities is called a relationship.
• Often shown as a Diagram.Entity Relationship Diagrams (ER Diagrams)
CAR
OWNER
Types of relationships
• One to one (1:1)
• One to many (1:n or 1:*)
• Many to Many (n:n *:*)
MAN
WOMEN
Is married to
MOTHER CHILD
PATIENT
TREATMENT
STUDENT
CLASS
ORDER
PRODUCT
Question
• A car has one registered owner with the DVLA• One person may own several vehicles• Draw an entity-relationship diagram to
represent this
OWNER CARis owner of
CAR OWNERis owned by
Question
• It is possible for a blind person to be given one guide dog
• A guide dog will look after just one blind person
• Draw the entity-relationship diagram to represent this
BLIND PERSON GUIDE DOGkeeps
guidesGUIDE DOG BLIND PERSON
Question • An item can be ordered by a customer• A customer may order as many items as is
needed• An item may appear on orders made by
different customers• Draw an entity-relationship diagram to
represent the item and the order
ITEM ORDERappears on
ORDER ITEMconsists of
Dealing with many-to many relationships
• Many-to-many relationships cannot be implemented easily in a database
• It is important to break up many to many relationships by creating an extra entity
ORDER
ITEM
ORDER
ITEM
ORDERITEM New Entity
Question
• A library lends out books to borrowers• Each borrower may borrow several books• There may be more than one copy of a book• Each book title is identified by its ISBN• Each book copy is identified by a unique
number• Draw the entity-relationship diagram to
represent this system
Answer
BORROWER
BOOKTITLE
BOOKCOPY
has
Answer
BORROWER
BOOKTITLE
BOOKCOPY
LOAN
makes of
has
Summary
• A relational database can be modelled using an entity-relationship diagram
• There are four degrees of relationships– one to one (1:1)– many to one (n:1)– one to many (1:n)– many to many (n:n)
• many-to-many relationship should be broken up as they cannot easily be represented in a database
Glossary
Database: a structured collection of data
Entity: an object or thing of interest about which data is recorded
Relationship: an association or link between two entities
Data model: a method of describing the data, it’s structure and the way it is interrelated
Attribute: A piece of data recorded about an entity