er modelling

40
ENTITY RELATIONSHIP MODELING COURSE INSTRUCTOR: MUNAWAR AHMAD Database Management Systems

Upload: abdur-rehman-muhammadi

Post on 19-Jun-2015

273 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Er modelling

ENTITY RELATIONSHIP MODELING

COURSE INSTRUCTOR: MUNAWAR AHMAD

Database Management Systems

Page 2: Er modelling

In This Lecture

Entity/Relationship models • Entities and Attributes • Relationships • Attributes • E/R Diagrams • For more information • Connolly and Begg chapter 11 • Ullman and Widom chapter 2

Page 3: Er modelling

Database Design

• Before we look athow to create anduse a database we’lllook at how todesign one• Need to consider• What tables, keys,and constraints areneeded?• What is the databasegoing to be used for?

• Conceptual design• Build a modelindependent of thechoice of DBMS• Logical design• Create the databasein a given DBMS• Physical design• How the database isstored in hardware

Page 4: Er modelling

Entity Relationship Modeling

• ER Modeling isused for conceptualdesign• Entities - objects oritems of interest• Attributes - factsabout, or propertiesof, an entity• Relationships - linksbetween entities

Page 5: Er modelling

Example

In a University database we might have entities for Students, Modules and Lecturers. Students might have attributes such as their ID, Name, and Course, and could have relationships with Modules (enrolment) and Lecturers (tutor/tutee)

Page 6: Er modelling

Entity/Relationship Diagram

ER Models are oftenrepresented as ERdiagrams that• Give a conceptualview of the database• Are independent ofthe choice of DBMS• Can identify someproblems in a design

Page 7: Er modelling

Entities

Entities representobjects or things ofinterest• Physical things likestudents, lecturers,employees, products• More abstract thingslike modules, orders,courses, projects

Entities have• A general type orclass, such asLecturer or Module• Instances of thatparticular type, suchas Steve Mills,Natasha areinstances of Lecturer• Attributes (such asname, email address)

Page 8: Er modelling

Diagramming Entities

In an ER Diagram,an entity is usuallydrawn as a box withrounded corners• The box is labeledwith the name of theclass of objectsrepresented by thatentity

Page 9: Er modelling

Attributes

Attributes are facts,aspects, properties,or details about anentity• Students have IDs,names, courses,addresses, …• Modules have codes,titles, credit weights,levels, …

Attributes have• A name• An associated entity• Domains of possiblevalues• Values from thedomain for eachinstance of the entitythey are belong to

Page 10: Er modelling

Diagramming Attributes

In an ER Diagramattributes may bedrawn as ovals• Each attribute islinked to its entity bya line• The name of theattribute is written inthe oval

Page 11: Er modelling

Relationships

Relationships are anassociation betweentwo or more entities• Each Student takesseveral Modules• Each Module is taughtby a Lecturer• Each Employee worksfor a singleDepartment

Relationships have• A name• A set of entities thatparticipate in them• A degree - thenumber of entitiesthat participate (mosthave degree 2)• A cardinality ratio

Page 12: Er modelling

Cardinality Ratios

• Each entity in arelationship canparticipate in zero,one, or more thanone instances of thatrelationship• This leads to 3 typesof relationship…

• One to one (1:1)• Each lecturer has aunique office• One to many (1:M)• A lecturer may tutormany students, but eachstudent has just onetutor• Many to many (M:M)• Each student takesseveral modules, andeach module is taken byseveral students

Page 13: Er modelling

Diagramming Relationships

• Relationships arelinks between twoentities• The name is given ina diamond box• The ends of the linkshow cardinality

Page 14: Er modelling

Removing M:M Relationships

• Many to manyrelationships aredifficult to represent• We can split a manyto many relationshipinto two one tomany relationships• An entity representsthe M:M relationship

Page 15: Er modelling

Removing M:M Relationships

• To make an ERmodel you need toidentify• Entities• Attributes• Relationships• Cardinality ratios• from a description

• General guidelines• Since entities arethings or objects theyare often nouns in thedescription• Attributes are facts orproperties, and so areoften nouns also• Verbs often describerelationships betweenentities

Page 16: Er modelling

Example

A university consists of a number ofdepartments. Each department offers severalcourses. A number of modules make up eachcourse. Students enroll in a particular courseand take modules towards the completion ofthat course. Each module is taught by alecturer from the appropriate department, andeach lecturer tutors a group of students

Page 17: Er modelling

Example - Entities

Page 18: Er modelling

Example - Relationships

Page 19: Er modelling

Example - ER Diagram

Entities: Department, Course, Module, Lecturer, Student

Page 20: Er modelling

Example - ER Diagram

Each department offers several courses

Page 21: Er modelling

Example - ER Diagram

A number of modules make up each courses

Page 22: Er modelling

Example - ER Diagram

Students enroll in a particular course

Page 23: Er modelling

Example - ER Diagram

Students … take modules

Page 24: Er modelling

Example - ER Diagram

Each module is taught by a lecturer

Page 25: Er modelling

Example - ER Diagram

a lecturer from the appropriate department

Page 26: Er modelling

Example - ER Diagram

each lecturer tutors a group of students

Page 27: Er modelling

Example - ER Diagram

Page 28: Er modelling

Entities and Attributes

• Sometimes it is hardto tell if somethingshould be an entityor an attribute• They both representobjects or facts aboutthe world• They are both oftenrepresented by nounsin descriptions

• General guidelines• Entities can haveattributes butattributes have nosmaller parts• Entities can haverelationships betweenthem, but an attributebelongs to a singleentity

Page 29: Er modelling

Example

We want to represent information aboutproducts in a database. Each producthas a description, a price and asupplier. Suppliers have addresses,phone numbers, and names. Eachaddress is made up of a street address,a city, and a postcode.

Page 30: Er modelling

Example - Entities/Attributes

• Entities or attributes:

• product• description• price• supplier• address• phone number• name• street address• city• postcode

• Products, suppliers,and addresses allhave smaller partsso we can makethem entities• The others have nosmaller parts andbelong to a singleentity

Page 31: Er modelling

Example - ER Diagram

Page 32: Er modelling

Example - Relationships

• Each product has asupplier

• Each product has asingle supplier butthere is nothing tostop a suppliersupplying manyproducts

• A many to onerelationship

• Each supplier has an address

• A supplier has a singleaddress• It does not seemsensible for twodifferent suppliers tohave the sameaddress

• A one to one relationship

Page 33: Er modelling

Example - ER Diagram

Page 34: Er modelling

One to One Relationships

• Some relationshipsbetween entities, Aand B, might beredundant if• It is a 1:1 relationshipbetween A and B• Every A is related to aB and every B isrelated to an A

• Example - thesupplier-addressrelationship• Is one to one• Every supplier has anaddress• We don’t needaddresses that are notrelated to a supplier

Page 35: Er modelling

Redundant Relationships

• We can merge thetwo entities thattake part in aredundantrelationship together• They become a singleentity• The new entity has allthe attributes of theold one

Page 36: Er modelling

Example - E/R Diagram

Page 37: Er modelling

Making E/R Diagrams

• From a description ofthe requirementsidentify the• Entities• Attributes• Relationships• Cardinality ratios ofthe relationships

• Draw the E/Rdiagram and then• Look at one to onerelationships as theymight be redundant• Look at many tomany relationships asthey might need to besplit into two one tomany links

Page 38: Er modelling

Debugging Designs

• With a bit of practiceER diagrams can beused to plan queries• You can look at thediagram and figureout how to find usefulinformation• If you can’t find theinformation you need,you may need tochange the design

How can youfind a list ofstudents whoare enrolledin Databasesystems?

Page 39: Er modelling

Debugging Designs

(3) For each instance of Enrolment in theresult of (2) find the corresponding Student(2) Find instances of the Enrolment entitywith the same Code as the result of (1)(1) Find the instance of the Moduleentity with title ‘Database Systems’

Page 40: Er modelling

Thank You