er modelling
TRANSCRIPT
ENTITY RELATIONSHIP MODELING
COURSE INSTRUCTOR: MUNAWAR AHMAD
Database Management Systems
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
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
Entity Relationship Modeling
• ER Modeling isused for conceptualdesign• Entities - objects oritems of interest• Attributes - factsabout, or propertiesof, an entity• Relationships - linksbetween entities
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)
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
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)
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
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
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
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
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
Diagramming Relationships
• Relationships arelinks between twoentities• The name is given ina diamond box• The ends of the linkshow cardinality
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
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
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
Example - Entities
Example - Relationships
Example - ER Diagram
Entities: Department, Course, Module, Lecturer, Student
Example - ER Diagram
Each department offers several courses
Example - ER Diagram
A number of modules make up each courses
Example - ER Diagram
Students enroll in a particular course
Example - ER Diagram
Students … take modules
Example - ER Diagram
Each module is taught by a lecturer
Example - ER Diagram
a lecturer from the appropriate department
Example - ER Diagram
each lecturer tutors a group of students
Example - ER Diagram
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
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.
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
Example - ER Diagram
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
Example - ER Diagram
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
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
Example - E/R Diagram
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
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?
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’
Thank You