08 - erd modelling

27
Section 08 ERD Modelling 1 08 - ERD Modelling HSQ - DATABASES & SQL And Franchise Colleges By MANSHA NAWAZ

Upload: aren

Post on 20-Jan-2016

140 views

Category:

Documents


0 download

DESCRIPTION

And Franchise Colleges. HSQ - DATABASES & SQL. 08 - ERD Modelling. By MANSHA NAWAZ. Entity Relationship Modelling Introduction. In this lecture we will try out a practical example of Entity-Relationship data modelling. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 08 - ERD Modelling

Section 08 ERD Modelling 1

08 - ERD Modelling

HSQ - DATABASES & SQL

And Franchise Colleges

By MANSHA NAWAZ

Page 2: 08 - ERD Modelling

Section 08 ERD Modelling 2

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.

Page 3: 08 - ERD Modelling

Section 08 ERD Modelling 3

ERD Examples

Example 1

ERD from DATASTORE

PO – PURCHASE ORDERS  

Example 2

ERD from DATASTORE

FLIGHTS  

Example 3

ERD from a CASE STUDY

FILM CLUB

Page 4: 08 - ERD Modelling

Section 08 ERD Modelling 4

UNISPARES PURCHASE ORDER FORM 99 Borough RD, Middlesbrough, TS1 3BA

tel: 01642-123456 fax: 01642-654321 PO No. 002594 PO Date: 15.12.96 Supplier Name: SELWOOD CONSULTANCY LTD Supplier Address: KINGSDAEL HOUSE MARINTET ROAD THORNABY TS17 0BB ORDER DETAILS : PART# PART DESCRIPTION QTY

CPU012 Pentium 100 16/1000 2 PRI6214 HP Deskjet 660C 3 MON023 Philips 14" Colour Monitor 1 CON061 HP Deskjet 660C Cartridge 4 CPU015 Pentium 166 32/2000 CD 1 KEY031 Extended Keyboard 10 CPU072 Pentium 120 16/1000 2

Payment Terms : COD/7days/30days/45days

In case of queries contact : FRED BLOGGS ext. 321

Example 1

ERD from DATASTORE

PO – PURCHASE ORDERS  

Page 5: 08 - ERD Modelling

Section 08 ERD Modelling 5

• ER MODEL : WORKED EXAMPLE

• MODELLING FROM DATASTORES ORDERS 1. Identify all entities in the above form.

ORDERS SUPPLIERSPARTSPAYMENT-TERMS (PT) * tabular list of payment termsCONTACT * 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)

Page 6: 08 - ERD Modelling

Section 08 ERD Modelling 6

3. Derive Entity Relationships

1 ORDER must have 1 SUPPLIER1 SUPPLIER may have 0,1,M ORDER

1 ORDER must have 1,M PARTS1 PART may be on have 0,1,M ORDER

1 ORDER must have 1 PT1 PT may be on have 0,1,M ORDER

1 ORDER must have 1 CONTACT1 CONTACT may be on have 0,1,M ORDER

4 Diagram (ERD) for the above showing entities of interest with their associated attributes, relationships and dependency.

Page 7: 08 - ERD Modelling

Section 08 ERD Modelling 7

(s#, sname, saddress, ...) s#, po# (po#, odate) po#, QTY, p# (p#, pdesc)

po#, c# po#, pt#

(pt#, ptdesc) (c#, cname, cext) ERD - ORDERS

ORDERS PARTS contains SUPPLIERS supplies

terms postaff

PAYMENT TERMS CONTACT

Page 8: 08 - ERD Modelling

Section 08 ERD Modelling 8

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#)

Page 9: 08 - ERD Modelling

Section 08 ERD Modelling 9

CONTACT (c#, cname, cext) 01 BLOGGS F. 321 supplies (s#, po#) 000001 002594

contains (p#, QTY, po#) CPU012 2 002594 PRI6214 3 002594 MON023 1 002594 CON061 4 002594 CPU015 1 002594 KEY031 1 002594 CPU072 2 002594

terms (po#, pt#) 002594 1 postaff (po#, c#) 002594 01

ORDERS (po#, odate) 002594 15.12.96 SUPPLIERS(s#, sname, saddress, .) 001 Selwood ….. PARTS (p#, pdesc, …..)

CPU012 Pentium 100 16/1000 PRI6214 HP Deskjet 660C MON023 Philips 14" Colour Monitor CON061 HP Deskjet 660C Cartridge CPU015 Pentium 166 32/2000 CD KEY031 Extended Keyboard CPU072 Pentium 120 16/1000

PAYMENT-TERMS (pt#, ptdesc) 1 COD 2 7 days 3 30 days 4 45 days

6. Populate your skeleton tables using the data provided in the above order form.

Page 10: 08 - ERD Modelling

Section 08 ERD Modelling 10

UNISPARES ORDER FORM

99 Borough RD Middlesbrough

TS1 3BA tel: 01642-123456 fax: 01642-654321

PO No. 002595 PO Date: 15.12.96 Supplier Name: JOE BLOGGS Computers Supplier Address: LINTHORPE RD MIDDLESBROUGH

TS1 3BA ORDER DETAILS : ITEM# PART# PART DESCRIPTION QTY 1 CPU012 Pentium 100 16/1000 10 2 PRI6214 HP Deskjet 660C 5 3 MON023 Philips 14" Colour Monitor 4 4 CON062 HP Deskjet 690C Cartridge 4 5 CPU016 Pentium 200 32/2000 CD 1 6 CPU074 Pentium Pro 150 16/1000 2 Payment Terms : COD/7days/30days/45days In case of queries, please contact : HARRY SMITH ext. 322

•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. 

Page 11: 08 - ERD Modelling

Section 08 ERD Modelling 11

Populate your skeleton tables using the data provided in the above order form.

ORDERS (po#, odate) 002594 15.12.96 002595 15.12.96 SUPPLIERS (s#, sname, saddress, …….) 000001 Selwood Consultancy Kingsdale House…… 000002 Joe Bloggs Computers Linthorpe Rd….. PARTS (p#, pdesc, …..)

CPU012 Pentium 100 16/1000 PRI6214 HP Deskjet 660C MON023 Philips 14" Colour Monitor CON061 HP Deskjet 660C Cartridge CON062 HP Deskjet 690C Cartridge CPU015 Pentium 166 32/2000 CD CPU016 Pentium 200 32/2000 CD CPU074 Pentium Pro 150 16/1000 KEY031 Extended Keyboard CPU072 Pentium 120 16/1000

Page 12: 08 - ERD Modelling

Section 08 ERD Modelling 12

terms(po#, pt#)002594 1 002595 3

contains(p#, QTY, po#)CPU012 2 002594PRI6214 3 002594MON023 1 002594CON061 4 002594CPU015 1 002594KEY031 10 002594CPU072 2 002594CPU012 10 002595PRI6214 5 002595MON023 4 002595CON062 4 002595CPU016 1 002595CPU074 2 002595

 PAYMENT-TERMS (pt#, ptdesc)1 COD2 7 days3 30 days4 45 days CONTACT(c#, cname, cext)01 BLOGGS FRED 32102 SMITH HARRY 322 supplies(s#, po#)000001 002594000002 002595

postaff(po#, c#)• 01• 002595 02

Page 13: 08 - ERD Modelling

Section 08 ERD Modelling 13

Airlines hold information about flights. Data is held as follows : Flight Aircraft Make Seats Airport City A-time D-timeBA069 747 BA 402 LHR London - 1300BA069 747 BA 402 ZRH Zurich 1430 1530BA069 747 BA 402 BAH Bahrain 2300 0015BA069 747 BA 402 SEZ Seychelles 0545 0645BA069 747 BA 402 MRU Mauritius 0910 - SK586 DC8 DC 123 LIS Lisbon - 1500SK586 DC8 DC 123 ZRH Zurich 1815 1855SK586 DC8 DC 123 CPH Copenhagen 2110 2145SK586 DC8 DC 123 ARN Stockholm 2255 - SK783 DC8 DC 123 CPH Copenhagen - 0940SK783 DC8 DC 123 ATH Athens 1400 1500SK783 DC8 DC 123 DAM Damascus 1700 - SK961 DC10 DC 230 CPH Copenhagen - 1810SK961 DC10 DC 230 ATH Athens 2030 0030SK961 DC10 DC 230 JNB Johannesburg 0935 - 

Example 2

ERD from DATASTORE

FLIGHTS  

Page 14: 08 - ERD Modelling

Section 08 ERD Modelling 14

ER MODEL : WORKED EXAMPLE

SOLUTION 1 : ERD FROM DATASTORES FLIGHTS

Page 15: 08 - ERD Modelling

Section 08 ERD Modelling 15

uses

carriesAircraft

Airport

Flight

Flight (flight#, arrival_airport, depart_airport, arrival_time, depart_time)

Airport (code, city)

Aircraft (aircraft, no_of_seats)

Identifier of flight seems strange. ‘Flight_no’ alone should identify a flight.

SOLUTION 2 : ERD FROM DATASTORES FLIGHTS

Page 16: 08 - ERD Modelling

Section 08 ERD Modelling 16

carries

Stops_at

Aircraft

Airport

Stopover

Flight (flight#, arrival_airport, depart_airport, arrival_time, depart_time)

Stopover (flight_no, code, arrival_time, depart_time)

Airport (code, city)

Aircraft (aircraft, no_of_seats)

Flight

Leaves_from

Departs_from

Arrives_at

SOLUTION 3 : ERD FROM DATASTORES FLIGHTS

Page 17: 08 - ERD Modelling

Section 08 ERD Modelling 17

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.

Example 3

ERD from a CASE STUDY

Page 18: 08 - ERD Modelling

Section 08 ERD Modelling 18

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.

Page 19: 08 - ERD Modelling

Section 08 ERD Modelling 19

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 FilmShows

m m

• 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.

Page 20: 08 - ERD Modelling

Section 08 ERD Modelling 20

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 FilmShows

m m

Cinema: 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.

Page 21: 08 - ERD Modelling

Section 08 ERD Modelling 21

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 FilmShows

m m

Showing Season

More entities ...

Showing: Attributes: date, time, takings?

and

• 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.

Page 22: 08 - ERD Modelling

Section 08 ERD Modelling 22

• 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 Filmat

1 mSeason

of

1m

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.

Page 23: 08 - ERD Modelling

Section 08 ERD Modelling 23

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!)

• Write out the enterprise rules for new relationship.

Cinema Film

at

1 mSeason

of

1m

Showing

in m

1

Page 24: 08 - ERD Modelling

Section 08 ERD Modelling 24

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.

Cinema Film

at

1 mSeason

of

1m

Showing

inm

1

• 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.

Page 25: 08 - ERD Modelling

Section 08 ERD Modelling 25

Identifiers

Showing 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.

and

Page 26: 08 - ERD Modelling

Section 08 ERD Modelling 26

ERD SAMPLES

Ascent Resources

Ascent S/W and ERD Solutions

Installing Ascent At Home

Using Ascent - ER Modeling

Library of Free Data Models

Page 27: 08 - ERD Modelling

Section 08 ERD Modelling 27

End of Lecture