Practical Modelling 1

advertisement
And Franchise Colleges
HSQ - DATABASES & SQL
08 - ERD Modelling
By MANSHA NAWAZ
Section 08
ERD Modelling
1
Entity Relationship Modelling Introduction
•
In this lecture we will try out a practical example of Entity-Relationship
data modelling.
•
Data Modelling is a very creative process - logic, intuition and
imagination are all needed.
•
At various stages you will need to attempt problems yourself so you will
need a pen and paper.
•
E-R data modelling is the subject of more than half the examination or ica
•
Modelling from datastore and a case study scenario in this lecture.
Section 08
ERD Modelling
2
ERD Examples
Example 1
Example 2
Example 3
ERD from DATASTORE
ERD from DATASTORE
ERD from a CASE STUDY
PO – PURCHASE ORDERS
FLIGHTS
FILM CLUB
Section 08
ERD Modelling
3
UNISPARES PURCHASE ORDER FORM
99 Borough RD, Middlesbrough, TS1 3BA
tel: 01642-123456 fax: 01642-654321
Example 1
PO No. 002594
ERD from DATASTORE
PO – PURCHASE ORDERS
Supplier Name:
Supplier Address:
PO Date: 15.12.96
SELWOOD CONSULTANCY LTD
KINGSDAEL HOUSE
MARINTET ROAD
THORNABY
TS17 0BB
ORDER DETAILS :
PART#
CPU012
PRI6214
MON023
CON061
CPU015
KEY031
CPU072
PART DESCRIPTION
Pentium 100 16/1000
HP Deskjet 660C
Philips 14" Colour Monitor
HP Deskjet 660C Cartridge
Pentium 166 32/2000 CD
Extended Keyboard
Pentium 120 16/1000
QTY
2
3
1
4
1
10
2
Payment Terms : COD/7days/30days/45days
In case of queries contact : FRED BLOGGS
Section 08
ERD Modelling
ext. 321
4
•
ER MODEL : WORKED EXAMPLE
•
MODELLING FROM DATASTORES ORDERS
1. Identify all entities in the above form.
ORDERS
SUPPLIERS
PARTS
PAYMENT-TERMS (PT)
CONTACT
* tabular list of payment terms
* tabular list of contact staff
2. Allocate main attributes identified.
ORDERS(po#, odate)
SUPPLIERS(s#, sname, saddress, …….)
PARTS(p#, pdesc, …..)
PT(pt#, ptdesc)
CONTACT(c#, cname, cext)
Section 08
ERD Modelling
5
3. Derive Entity Relationships
1 ORDER must have 1 SUPPLIER
1 SUPPLIER may have 0,1,M ORDER
1 ORDER must have 1,M PARTS
1 PART may be on have 0,1,M ORDER
1 ORDER must have 1 PT
1 PT may be on have 0,1,M ORDER
1 ORDER must have 1 CONTACT
1 CONTACT may be on have 0,1,M ORDER
4
Section 08
Diagram (ERD) for the above showing entities of
interest with their associated attributes,
relationships and dependency.
ERD Modelling
6
s#,...)po#
(s#, sname, saddress,
supplies
SUPPLIERS
(po#, odate)
po#, QTY, p# (p#, pdesc)
contains
ORDERS
PARTS
postaff
terms
po#, pt#
po#, c#
PAYMENT TERMS
CONTACT
ERD - ORDERS
(pt#, ptdesc)
Section 08
ERD Modelling
(c#, cname, cext)
7
5.
List a set of skeleton tables derived from your model
ORDERS(po#, odate)
SUPPLIERS(s#, sname, saddress, …….)
PARTS(p#, pdesc, …..)
PAYMENT-TERMS(pt#, ptdesc)
CONTACT(c#, cname, cext)
supplies(s#, po#)
contains(p#, QTY, po#)
terms(po#, pt#)
postaff(po#, c#)
Section 08
ERD Modelling
8
6. Populate your skeleton tables using the data provided in the above order form.
ORDERS (po#,
002594
odate)
15.12.96
SUPPLIERS(s#, sname,
001 Selwood
PARTS
(p#,
CPU012
PRI6214
MON023
CON061
CPU015
KEY031
CPU072
PAYMENT-TERMS
Section 08
CONTACT (c#,
01
saddress, .)
…..
pdesc, …..)
Pentium 100 16/1000
HP Deskjet 660C
Philips 14" Colour Monitor
HP Deskjet 660C Cartridge
Pentium 166 32/2000 CD
Extended Keyboard
Pentium 120 16/1000
(pt#,
1
2
3
4
ptdesc)
COD
7 days
30 days
45 days
ERD Modelling
cname,
BLOGGS F.
supplies (s#,
000001
contains (p#,
CPU012
PRI6214
MON023
CON061
CPU015
KEY031
CPU072
terms
(po#,
002594
postaff
(po#,
002594
cext)
321
po#)
002594
QTY,
2
3
1
4
1
1
2
po#)
002594
002594
002594
002594
002594
002594
002594
pt#)
1
c#)
01
9
UNISPARES ORDER FORM
99 Borough RD
Middlesbrough
TS1 3BA
tel: 01642-123456 fax: 01642-654321
•The previous data set is only a
partial view.
•Additional orders can be
generated and added to the
database.
•The next slide shows how the
sample order (figure A-2) affects
our database.
•Note : No REDUNDANT or
DUPLICATED data in tables
which show strong data
INTEGRITY.
PO No. 002595
Supplier Name:
Supplier Address:
PO Date: 15.12.96
JOE BLOGGS Computers
LINTHORPE RD
MIDDLESBROUGH
TS1 3BA
ORDER DETAILS :
ITEM# PART#
1
CPU012
2
PRI6214
3
MON023
4
CON062
5
CPU016
6
CPU074
PART DESCRIPTION
Pentium 100 16/1000
HP Deskjet 660C
Philips 14" Colour Monitor
HP Deskjet 690C Cartridge
Pentium 200 32/2000 CD
Pentium Pro 150 16/1000
QTY
10
5
4
4
1
2
Payment Terms : COD/7days/30days/45days
Section 08
ERD Modelling
In case of queries, please contact : HARRY SMITH
10
ext. 322
Populate your skeleton tables using the data provided in the above order
form.
ORDERS (po#,
002594
002595
odate)
15.12.96
15.12.96
SUPPLIERS
sname,
Selwood Consultancy
Joe Bloggs Computers
PARTS
Section 08
(s#,
000001
000002
(p#,
CPU012
PRI6214
MON023
CON061
CON062
CPU015
CPU016
CPU074
KEY031
CPU072
saddress, …….)
Kingsdale House……
Linthorpe Rd…..
pdesc, …..)
Pentium 100 16/1000
HP Deskjet 660C
Philips 14" Colour Monitor
HP Deskjet 660C Cartridge
HP Deskjet 690C Cartridge
Pentium 166 32/2000 CD
Pentium 200 32/2000 CD
Pentium Pro 150 16/1000
Extended Keyboard
Pentium 120 16/1000
ERD Modelling
11
PAYMENT-TERMS
(pt#,
ptdesc)
1 COD
2 7 days
3 30 days
4 45 days
CONTACT
(c#,
cname,
01 BLOGGS FRED 321
02 SMITH HARRY 322
supplies
(s#,
000001
000002
cext)
po#)
002594
002595
postaff
(po#,
c#)
259401
2595002595 02
Section 08
contains
(p#,
QTY,
CPU012 2
PRI6214 3
MON023 1
CON061 4
CPU015 1
KEY031 10
CPU072 2
CPU012 10
PRI6214 5
MON023 4
CON062 4
CPU016 1
CPU074 2
terms
(po#,
002594
002595
ERD Modelling
po#)
002594
002594
002594
002594
002594
002594
002594
002595
002595
002595
002595
002595
002595
pt#)
1
3
12
Airlines hold information about flights. Data is held as follows :
Example 2
Flight
BA069
ERD from DATASTORE BA069
BA069
BA069
FLIGHTS
BA069
Aircraft Make
747
BA
747
BA
747
BA
747
BA
747
BA
Seats
402
402
402
402
402
Airport
LHR
ZRH
BAH
SEZ
MRU
City
London
Zurich
Bahrain
Seychelles
Mauritius
A-time
1430
2300
0545
0910
D-time
1300
1530
0015
0645
-
DC8
DC8
DC8
DC8
DC
DC
DC
DC
123
123
123
123
LIS
ZRH
CPH
ARN
Lisbon
Zurich
Copenhagen
Stockholm
1815
2110
2255
1500
1855
2145
-
SK783 DC8
SK783 DC8
SK783 DC8
DC
DC
DC
123
123
123
CPH
ATH
DAM
Copenhagen
Athens
Damascus
1400
1700
0940
1500
-
SK961 DC10
SK961 DC10
SK961 DC10
DC
DC
DC
230
230
230
CPH
ATH
JNB
Copenhagen Athens
2030
Johannesburg 0935
1810
0030
-
SK586
SK586
SK586
SK586
Section 08
ERD Modelling
13
ER MODEL : WORKED EXAMPLE
SOLUTION 1 : ERD FROM DATASTORES FLIGHTS
Section 08
ERD Modelling
14
SOLUTION 2 : ERD FROM DATASTORES FLIGHTS
carries
Aircraft
Flight
Flight
(flight#, arrival_airport, depart_airport, arrival_time,
depart_time)
uses
Airport (code, city)
Aircraft (aircraft, no_of_seats)
Identifier of flight seems strange. ‘Flight_no’ alone should
identify a flight.
Airport
Section 08
ERD Modelling
15
SOLUTION 3 : ERD FROM DATASTORES FLIGHTS
Aircraft
Flight (flight#, arrival_airport, depart_airport,
arrival_time, depart_time)
Stopover (flight_no, code, arrival_time, depart_time)
carries
Airport (code, city)
Aircraft (aircraft, no_of_seats)
Stopover
Flight
Departs_from
Stops_at
Leaves_from
Arrives_at
Airport
Section 08
ERD Modelling
16
Example 3
ERD from a CASE STUDY
Film Club Case Study:
Film Club UK is a company that owns or leases a number of
small cinemas in the UK. They have commissioned a
database designer to design a database solution to enable
them to maintain details about their cinemas and the films
that they show. Note that it is possible to have two cinemas in
the same location with the same name (there used to be two
Odeons in Newcastle). It is also possible to have different
films with the same title (for example, different versions of a
Shakespeare play).
Films are scheduled for one or more showings at a cinema
within a ‘season’. Season details are to be notified in
advance of the dates and times of showings, takings, etc. to
be notified later.
Any one film may have more than one season at any one
cinema (for example, a cinema showing ‘The Snowman’ each
Christmas). At present, all cinemas are single-screen.
Section 08
ERD Modelling
17
Queries:
– Details of number of employees (if any – some cinemas are small and manned
by volunteers) at a cinema
– Analyse takings and numbers of showings for films with times and dates of
showings
– List cinemas by seating capacity
– List films shown anywhere since a certain date
– List and summarise films by classification.
Section 08
ERD Modelling
18
Film Club UK is a company that owns or leases a number of small cinemas in the UK.
They have commissioned a database designer to design a database solution to enable
them to maintain details about their cinemas and the films that they show.
Entities (and initial thoughts on relationships)
Cinema
•
m
m
Shows
Film
What does this ERD say about Cinemas and Films?
– Write down the enterprise rules shown on the diagram (0,1,m etc.)
•
This looks sensible but we need to know more about cinemas and films.
•
Just because it looks good does not mean it is correct.
– We have not looked at dealing with m:m relationships yet - very complicated.
•
A good start - but what next?
– Find attributes and identifier for these entities.
Section 08
ERD Modelling
19
Note that it is possible to have two cinemas in the same location with the same name
(there used to be two Odeons in Leicester Square). It is also possible to have different
films with the same title (for example, different versions of a Shakespeare play).
Cinema
Cinema:
m
m
Shows
Film
Identifier: Cinema_id
Attributes: Cinema_name, location, address, ….
Film:
Identifier: Film_id
Attributes: title, duration, category, ….
•
•
•
Do these make good sense?
Could we have missed things at this stage?
Clearly an iterative process.
Section 08
ERD Modelling
20
Films are scheduled for one or more showings at a cinema within a ‘season’. Season
details are to be notified in advance of the dates and times of showings, takings, etc. to
be notified later.
Cinema
m
m
Shows
Film
More entities ...
Showing
and
Season
Showing: Attributes: date, time, takings?
•
Try to redraw the E-R diagram including these 2 entities.
•
A useful trick: Where is the ‘money’?
– Working backwards from entities that are key business things (money?) can
work well.
Section 08
ERD Modelling
21
•
•
Write out the enterprise rules for this version.
Notice the direction of the relationship names.
– The direction is away from the ‘business entity.
– You can get a lot out of ‘Where is the money?’ and ‘Where is the business?’
Cinema
1
m
at
Season
m
of
1
Film
We can add showing later ...
A cinema has 0,1,m season(s)
A season is at exactly 1 cinema
A film is shown in 0,1,m season(s)
A season of exactly 1 film
•
So what about the showing entity?
•
Try to add the showing entity to your ERD.
Section 08
ERD Modelling
22
•
Write out the enterprise rules for new relationship.
Cinema
1
m
at
Season
1
m
of
Film
1
in
m
Showing
A season involves 0,1,m showing(s)
A showing is in exactly 1 season
•
•
•
Why not relate showing to film? It is a showing of a film …
All the showings in a season are the same film.
Could you have a season that does not have any showings?
(yet!)
Section 08
ERD Modelling
23
Any one film may have more than one season at any one cinema (for example, a
cinema showing ‘The Snowman’ each Christmas). At present, all cinemas are singlescreen.
Cinema
1
at
m
Season
m
of
1
Film
1
in
m
Showing
•
Have we coped with this already?
•
Will the model cope with multi-screen cinemas?
•
We would need a screen entity - try this for yourself later.
Section 08
ERD Modelling
24
Identifiers
Showing
and
Season
Season: Identifier: season_id or film, cinema, start_date ??
Attributes: start_date, end_date, total_takings
Showing: Identifier: season_id, date, time or showing_id
Attributes: takings, adult_tickets, child_tickets,
concession_tickets, free_passes, ……
Is total_takings redundant?
•This is a derived attribute - explained later.
Section 08
ERD Modelling
25
ERD SAMPLES
Ascent Resources
Ascent S/W and ERD Solutions
Installing Ascent At Home
Using Ascent - ER Modeling
Library of Free Data Models
Section 08
ERD Modelling
26
End of Lecture
Section 08
ERD Modelling
27
Download