Sie sind auf Seite 1von 27

Lesson Objectives

By the end of this lesson you will be able to:


1. Assign appropriate primary keys/key fields
2. Identify foreign keys in different
tables/files
3. Describe the key term relationships
4. Create relational databases

Recap of Key Terms


Field a single
item of data

Record all data


relating to one
thing or person
Studen
t
numbe
r

Surnam
e

1042

Jones

1043
1044

First
name

D.O.B.

Addres
s

Phone
number

Emma

05/04/95

123 High
Street

01234
765555

Smith

Louise

12/08/95

45 Low
Road

01234
688674

White

Simon

26/06/95

67
Middle
Lane

01234
984532

File collection of
related records

Illnesse
s

Allergie
s

Nuts
Diabetes

Doctor

Dr.
Brown
Dr.
Green

Wasp
stings

Dr.
Yellow

Continued
What is a key field?
A unique piece of data in a record also known as
a
primary key.
What is a flat file database?
A database which consists of only one table.
What is a relational database?
A database which consists of more than one
table.

Continued
ID

Title

Director

Lead Actor

Certificate

01

The Shawshank Redemption

Frank Darabont

Tim Robbins

15

02

The Godfather

Francis Ford Coppola

Marlon Brando

15

03

Inception

Christopher Nolan

Leonardo DiCaprio

12A

04

Pulp Fiction

Quentin Tarantino

John Travolta

18

05

Schindler's List

Steven Spielberg

Liam Neeson

15

Primary Key

A field in the table which allows each record


to be uniquely identified.
Databases only work if each record is
different. Primary Keys sort this out!
E.g. ID, Car Number Plate, National Insurance
Number.

Suitable or Not
In your books write down whether or not each of
the
following would be suitable primary keys?
Not
Date of BirthSuitable

Suitabl
Employee Number
e
Suitabl
Student IDe
Not
Surname Suitable

Not
Postcode Suitable

Suitabl
Car Number Plate
e

Example A Flat File


Database
Student

e
l
i
F
t
a
l
F
e
l
t
p
n
e
m
d
a
If students
are in the
u
x
t
E
S
same
tutor
group which
e
e
s
e
fields
would contain data
a
S
b
a
that would be repeated?
t
a
D
Student
Number

Surname

Forename

Date of Birth
Tutor Group

Staff Number
Staff Title

Staff Initial

Staff Surname

Which is the primary


key?

Linking Tables
In relational databases the same data is stored
more efficiently in more than one table. These
tables are linked together.
This helps save time as you no longer need to type
in the
same data over
and over Tutor
again known
as
Student
Tutors
Students
Groups
Student
reducing
data redundancy.
Staff
Student
Number

Number

Tutor Group

Number

Surname

Surname

Staff Number

Staff Title

Forename

Forename

Staff Initial

Date of Birth

Date of Birth

Tutor Group

Tutor Group

Staff
Surname

Staff Number
Staff Title
Staff Initial
Tutor Surname

Relationships
To link tables together there needs to be
the same field in each table, which is
usually the primary key.
Students
Student
Number
Surname
Forename
Date of Birth
Tutor Group

Tutor
Groups
Tutor Group
Staff
Number

Tutors
Staff Number
Staff Title
Staff Initial
Staff
Surname

Which are the primary


keys?

Foreign Keys
A foreign key is the primary key of another table.
Foreign keys are used to establish relationships between tables.
In the example the field Tutor Group would be the key field or
primary key in the Tutor Groups table and a foreign key in the
Students table.

Students
Student
Number
Surname
Forename
Date of Birth
Tutor Group

Foreign

Tutor
Groups
Tutor Group
Staff
Number

Primary
Key

Relationships
Primary Keys
Students

Tutor
Groups

Student
Number

Tutor Group

Surname

Staff ID

Staf

t
n
e
e
d
s
u
a
t
b
S
Which
are the ta
e
a
e
foreign
keys?
D
S
l
a
n
e
l
o
i
p
t
a
m
l
a
x
Re
E

Forename

Date of Birth
Tutor Group

Staff ID

Staff Title

Staff Initial
Staff
Surname

To link the Students table to the Tutor Groups table we


can use the Tutor Group field as it is in both tables.

Similarly, the Tutor Groups table and Staff table can be


linked through the Staff ID field.

Task
Create the database structure for a DVD Rental Shop.
You will need to store the following information and assign
appropriate primary and foreign keys.
Length of Film Forename
Year Released Film Title
E-mail
Date Rented
Rating
Address
Rental Price
Age
Surname Date Returned
Phone Number Film Genre
Gender Postcode
Step 1 Divide the fields up into separate categories there
will be 3 tables.

The Tables
Customer

Rentals

Film

Surname
CustomerID

RentalID
Date
Rented

Film
Title
FilmID

Forename
Surname

Rented
Date Returned

Rating
Film Title

Address
Forename

Date Returned

Film
Genre
Rating

Postcode
Address

Date
Released
Film Genre

Telephone
Postcode
Number
Telephone

Length
of Film
Date Released

E-mail
Number
Age
E-mail
Gender
Age

Rental
LengthPrice
of Film
Rental Price

Gender

Step 2 Add a primary key to each table.


What would be appropriate primary keys?

Linking the Tables


Customer

Rentals

Film

CustomerID

RentalID

FilmID

Surname

Date Rented

Film Title

Forename

Date Returned

Rating

Address

Film Genre

Postcode

Date Released

Telephone
Number

Length of Film

E-mail

Rental Price

Age
Gender

Step 3 Link the tables together by


assigning appropriate foreign keys.

Linking the Tables


Customer

Rentals

Film

CustomerID

RentalID

FilmID

Surname

Date Rented

Film Title

Forename

Date Returned

Rating

Address

CustomerID

Film Genre

Postcode

FilmID

Date Released

Telephone
Number
E-mail

Length of Film
Rental Price

Age
Gender

This now means the DVD shop can clearly


identify which films have been rented by each
of their customers without having to repeat the
same information.

The same but diferent


Customer
CustomerID

Customer

Surname

CustomerID

RentalID

Forename

Surname

Date Rented

Address

Forename

Postcode

Address

Telephone Number

Postcode

E-mail
Age

Telephone
Number

Gender

E-mail

Date Rented

Age

Date Returned

Film

Rentals

Date Returned
CustomerID
FilmID

FilmID
Film Title
Rating
Film Genre
Date Released
Length of Film
Rental Price

Gender

Film Title
Rating
Film Genre
Date Released
Length of Film
Rental Price

Looking at these two


structures
which one is better and why?

Creating a database
Have a go at creating the Rental
database...
Your teacher will show you how to:
Create database tables
Add fields
Assign appropriate data types
Dont include the Gender field in the
Customer table.
We will add foreign keys later.

Your 3 tables should look


like this
Customer

Rentals

Film

CustomerID

RentalID

FilmID

Surname

Date Rented

Film Title

Forename

Date Returned

Rating

Address

Film Genre

Postcode

Date Released

Telephone
Number

Length of Film
Rental Price

E-mail
Age

As a class go through and check you have


assigned the correct data type to each field.

Types of Relationships
There are 2 types of relationships:
One-to-One
One-to-Many

What do you think these terms mean?

One-to-One
A one-to-one relationship might be better
explained with the example of couple A
husband and a wife.
A husband can only have one wife.
A wife can only have one husband.

Husban
d

Wife

One-to-Many
Think about a mother and her children.
A mother can have many children.
A child can only have one biological mother.

Mother
Many
Symbol

Child

Linking the Tables


Your teacher will show you how to create
relationships.
Watch carefully, then have a go
Customer

Rentals

Film

CustomerID

RentalID

FilmID

Surname

Date Rented

Film Title

Forename

Date Returned

Rating

Address

CustomerID

Film Genre

Postcode

FilmID

Date Released

Telephone
Number

Data Type Number


not AutoNumber

Length of Film

E-mail
Age

Rental Price

From scratch
Create a database structure.
You will need to:
Create tables and add fields to each table
Assign appropriate data types
Create one-to-one/one-to-many
relationships as appropriate for the system

In your book write down the


relationships you added and explain
why

Museum Tour Booking

For each Customer you need to store:


System

Customer ID, Surname, Forename, Address Line 1,


Address Line 2, Address Line 3, Postcode, Telephone
Number, Email Updates Required

For each Booking you need to store:


Booking ID, Booking Date, Number of Adults, Number
of
Children
For each Tour you need to store:
Tour ID, Tour Name, Tour Guide, Tour Time, Adult Price,
Child Price
Think about how you will link the 3 tables together.

Answer

Plenary Exam
Questions
Give three features of a relational
database that are not found in a
flat file database.
Three from:
[3 marks]

More than one table/file


Tables are linked
Primary keys are needed
Foreign keys exist
Less duplication of data
Data from more than one table can be
used in the same report

Plenary Exam
Questions
Explain the benefits of using a relational
database over flat file database. [2
marks]

Two from:

Avoids data being duplicated


Data can be added and removed
easily
Easier to change data
Avoids inconsistent records
Easier to maintain security
Why are all of the above points benefits

Review Objectives
By the end of this lesson you will be able to:
1. Assign appropriate primary keys/key fields
2. Identify foreign keys in different
tables/files
3. Describe the key term relationships
4. Create relational databases

Das könnte Ihnen auch gefallen