Upload
torp42
View
915
Download
1
Embed Size (px)
Citation preview
Entity-Relationship Diagrams (ERD)
Kristian Torp
Department of Computer ScienceAalborg University
people.cs.aau.dk/˜[email protected]
November 5, 2015
daisy.aau.dk
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 1 / 40
Outline
1 Introduction
2 Basic StructuresEntity SetsRelationship SetsAttributesWeak Entities
3 Bad and Ugly Designs
4 ERD to Tables
5 Summary
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 2 / 40
Learning Goals
MotivationER-diagrams are widely usedER-diagrams are simple to learn
Much simpler than UML
An ER-diagram is a good communication tool
GoalsCreate non-trivial ER-diagrams
Analyze if an ER-diagram is good or bad
Create and explain the mapping of ER-diagrams to tables
Know that multiple ER-diagram notations exist
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 3 / 40
Outline
1 Introduction
2 Basic StructuresEntity SetsRelationship SetsAttributesWeak Entities
3 Bad and Ugly Designs
4 ERD to Tables
5 Summary
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 4 / 40
ER-Diagram Example
Student
Name StudentId
Address
course
CourseId
Description
ECTS Syllabus
Takes
Grade
StartDate
StudentTakes
Name
Entity Type Relationship Type Attribute
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 5 / 40
Outline
1 Introduction
2 Basic StructuresEntity SetsRelationship SetsAttributesWeak Entities
3 Bad and Ugly Designs
4 ERD to Tables
5 Summary
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 6 / 40
Outline
1 Introduction
2 Basic StructuresEntity SetsRelationship SetsAttributesWeak Entities
3 Bad and Ugly Designs
4 ERD to Tables
5 Summary
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 7 / 40
Examples Entity Sets
computer
CID
Make
Model
order
OID Amount
NoteNouns in the requirement specification
ContentShould have a primary key
Should have a set of uniquely named attributes
Should have a unique name in the ER diagram
Should make sense to the customer
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 8 / 40
Entity vs. Entity Set
Entity ≈ object ≈ an instance ≈ rowEntity set ≈ class ≈ a schema ≈ table
NoteOften we just say entity when we mean entity set!
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 9 / 40
Outline
1 Introduction
2 Basic StructuresEntity SetsRelationship SetsAttributesWeak Entities
3 Bad and Ugly Designs
4 ERD to Tables
5 Summary
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 10 / 40
Examples Relationship Sets
order order linehas
car gas stationtanking
computer motherboard cpuhas in
NoteVerbs in the requirement specification
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 11 / 40
Cardinalities
Student CourseTakes
Student CourseTakes
Student CourseTakes
Student CourseTakes
n:m
1:n
n:1
1:1
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 12 / 40
Quiz
computer motherboard cpuhas in
QuestionHow many motherboards can a computer have?
many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
QuestionHow many times can you tank your car? many times
Association or aggregation from UML? association
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
Quiz
computer motherboard cpuhas in
QuestionHow many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
QuestionHow many times can you tank your car? many times
Association or aggregation from UML? association
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
Quiz
computer motherboard cpuhas in
QuestionHow many motherboards can a computer have? many
Does the ERD support multi CPU computers?
yes
Association or aggregation from UML? aggregation
car gas stationtanking
QuestionHow many times can you tank your car? many times
Association or aggregation from UML? association
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
Quiz
computer motherboard cpuhas in
QuestionHow many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
QuestionHow many times can you tank your car? many times
Association or aggregation from UML? association
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
Quiz
computer motherboard cpuhas in
QuestionHow many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML?
aggregation
car gas stationtanking
QuestionHow many times can you tank your car? many times
Association or aggregation from UML? association
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
Quiz
computer motherboard cpuhas in
QuestionHow many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
QuestionHow many times can you tank your car? many times
Association or aggregation from UML? association
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
Quiz
computer motherboard cpuhas in
QuestionHow many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
QuestionHow many times can you tank your car?
many times
Association or aggregation from UML? association
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
Quiz
computer motherboard cpuhas in
QuestionHow many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
QuestionHow many times can you tank your car? many times
Association or aggregation from UML? association
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
Quiz
computer motherboard cpuhas in
QuestionHow many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
QuestionHow many times can you tank your car? many times
Association or aggregation from UML?
association
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
Quiz
computer motherboard cpuhas in
QuestionHow many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
QuestionHow many times can you tank your car? many times
Association or aggregation from UML? association
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
Participation
Student CourseTakes
Partial ParticipationPartial participation both entities
Student CourseTakes
Full participationFull participation student
All students have to take at least one course
Partial participation courseA course does not need to have students following, e.g., a new course
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 14 / 40
Participation
Student CourseTakes
Partial ParticipationPartial participation both entities
Student CourseTakes
Full participationFull participation student
All students have to take at least one course
Partial participation courseA course does not need to have students following, e.g., a new course
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 14 / 40
Participation Quiz
computer motherboard cpuhas in
QuestionCan the shop sell computers without motherboards?
yes
Can the shop sell motherboards without CPUs? no
Can the shop sell a separate CPU? no
Loan Paymentfor
QuestionDo we always know which payment a loan is for? yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
Participation Quiz
computer motherboard cpuhas in
QuestionCan the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs? no
Can the shop sell a separate CPU? no
Loan Paymentfor
QuestionDo we always know which payment a loan is for? yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
Participation Quiz
computer motherboard cpuhas in
QuestionCan the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs?
no
Can the shop sell a separate CPU? no
Loan Paymentfor
QuestionDo we always know which payment a loan is for? yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
Participation Quiz
computer motherboard cpuhas in
QuestionCan the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs? no
Can the shop sell a separate CPU? no
Loan Paymentfor
QuestionDo we always know which payment a loan is for? yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
Participation Quiz
computer motherboard cpuhas in
QuestionCan the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs? no
Can the shop sell a separate CPU?
no
Loan Paymentfor
QuestionDo we always know which payment a loan is for? yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
Participation Quiz
computer motherboard cpuhas in
QuestionCan the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs? no
Can the shop sell a separate CPU? no
Loan Paymentfor
QuestionDo we always know which payment a loan is for? yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
Participation Quiz
computer motherboard cpuhas in
QuestionCan the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs? no
Can the shop sell a separate CPU? no
Loan Paymentfor
QuestionDo we always know which payment a loan is for?
yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
Participation Quiz
computer motherboard cpuhas in
QuestionCan the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs? no
Can the shop sell a separate CPU? no
Loan Paymentfor
QuestionDo we always know which payment a loan is for? yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
Role
Self ReferenceAn entity set may be related to itself
Assume each teacher has a manager that is another teacher
teacher works for
manager
manages
NoteCan be read as (but never draw this)!
teacher teacherworks formanager manages
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 16 / 40
n-Ary Relationships
employee topic
meeting
discuss
CommentAt a number of meetings, a number of employees discuss a numberof topics
The same syntax is used in UML (classes not entity sets)
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 17 / 40
Outline
1 Introduction
2 Basic StructuresEntity SetsRelationship SetsAttributesWeak Entities
3 Bad and Ugly Designs
4 ERD to Tables
5 Summary
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 18 / 40
Examples
student
name
student
address
street no zip
Single Composite
student
birthdate age
student
Derived Multi-Valued
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 19 / 40
Relationship Sets and Attributes
car
ID Make Model
gas station
ID address
tanking
DateTime
QuestionWhy the DateTime on the tanking relationship set?
The characteristics of the entity and relationship sets in therequirement specification
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 20 / 40
Outline
1 Introduction
2 Basic StructuresEntity SetsRelationship SetsAttributesWeak Entities
3 Bad and Ugly Designs
4 ERD to Tables
5 Summary
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 21 / 40
Examples
order order linehas
QuestionAre order line depending on order?
No not in this ERD
Update DiagramMake order lines depend on an order
order
OID
order line
line no
has
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 22 / 40
Examples
order order linehas
QuestionAre order line depending on order? No not in this ERD
Update DiagramMake order lines depend on an order
order
OID
order line
line no
has
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 22 / 40
Examples
order order linehas
QuestionAre order line depending on order? No not in this ERD
Update DiagramMake order lines depend on an order
order
OID
order line
line no
has
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 22 / 40
Summary: Basic ConceptsConcepts
Entity setStrongWeak (no key)
Relationship setCardinality: 1-1, 1-n, n-mParticipation: Partial or fulln-ary (be careful)
AttributeSingleCompositeDerivedMulti-valued
NoteQuite simple to draw and use
Many alternative notations
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 23 / 40
Outline
1 Introduction
2 Basic StructuresEntity SetsRelationship SetsAttributesWeak Entities
3 Bad and Ugly Designs
4 ERD to Tables
5 Summary
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 24 / 40
Bad Design, One
Student
sid cid name
Course
cid name
takes
Bad: Course ID (cid) on both entities. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 25 / 40
Bad Design, One
Student
sid cid name
Course
cid name
takes
Bad: Course ID (cid) on both entities. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 25 / 40
Bad Design, One
Student
sid cid name
Course
cid name
takes
Bad: Course ID (cid) on both entities. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 25 / 40
Bad Design, Two
Teacher
tid name room
Course
name ects
gives
Bad: No primary key on the course entity. Fixed below!
Teacher
tid name room
Course
cid name ects
gives
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 26 / 40
Bad Design, Two
Teacher
tid name room
Course
name ects
gives
Bad: No primary key on the course entity. Fixed below!
Teacher
tid name room
Course
cid name ects
gives
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 26 / 40
Bad Design, Two
Teacher
tid name room
Course
name ects
gives
Bad: No primary key on the course entity. Fixed below!
Teacher
tid name room
Course
cid name ects
gives
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 26 / 40
Bad Design, Three
Student
sid name
Course
cid name
takes
Bad: Not connected to the course entity. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 27 / 40
Bad Design, Three
Student
sid name
Course
cid name
takes
Bad: Not connected to the course entity. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 27 / 40
Bad Design, Three
Student
sid name
Course
cid name
takes
Bad: Not connected to the course entity. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 27 / 40
Bad Design, Four
Student
sid name
Course
cid name
takes
sid cid
Bad: IDs from entities on relationship. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 28 / 40
Bad Design, Four
Student
sid name
Course
cid name
takes
sid cid
Bad: IDs from entities on relationship. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 28 / 40
Bad Design, Four
Student
sid name
Course
cid name
takes
sid cid
Bad: IDs from entities on relationship. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 28 / 40
Bad Design, Five
Student
sid name
Course
cid name
takes
Good — just checking
Student
sid
Course
cid name
takes
Bad: Only key attribute on Student entity.
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 29 / 40
Bad Design, Five
Student
sid name
Course
cid name
takes
Good — just checking
Student
sid
Course
cid name
takes
Bad: Only key attribute on Student entity.
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 29 / 40
Bad Design, Five
Student
sid name
Course
cid name
takes
Good — just checking
Student
sid
Course
cid name
takes
Bad: Only key attribute on Student entity.
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 29 / 40
Bad Design, Five
Student
sid name
Course
cid name
takes
Good — just checking
Student
sid
Course
cid name
takes
Bad: Only key attribute on Student entity.
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 29 / 40
Bad Design, Six
Student
sid name
Course
cid name
takes
Bad: This does not make sense with only weak entities. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 30 / 40
Bad Design, Six
Student
sid name
Course
cid name
takes
Bad: This does not make sense with only weak entities. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 30 / 40
Bad Design, Six
Student
sid name
Course
cid name
takes
Bad: This does not make sense with only weak entities. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 30 / 40
Bad Design, Seven
Student Course
cid name
takes
Bad: No attributes on student. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 31 / 40
Bad Design, Seven
Student Course
cid name
takes
Bad: No attributes on student. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 31 / 40
Bad Design, Seven
Student Course
cid name
takes
Bad: No attributes on student. Fixed below!
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 31 / 40
Outline
1 Introduction
2 Basic StructuresEntity SetsRelationship SetsAttributesWeak Entities
3 Bad and Ugly Designs
4 ERD to Tables
5 Summary
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 32 / 40
Three Step Overview
1 Convert all entity sets to tables, a column for each attribute2 Convert all relationship sets to tables, a column for the primary key of
the participation entity sets3 A lot of details
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 33 / 40
Many-to-Many
A student can take many courses, a course can have many students.
Student
sid name
Course
cid name
takes
create tab l e s tudent (s id i n t e g e r pr imary key ,name varchar2 (30) not n u l l )
c reate tab l e takes (s id i n t e g e r
re ferences student ( s id ) ,c id i n t e g e r
re ferences course ( c id ) ,pr imary key ( s id , c id ) )
c reate tab l e course (c id i n t e g e r pr imary key ,name varchar2 (30) not n u l l )
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 34 / 40
One-to-Many
A student can take many courses, a course can only have one student
Student
sid name
Course
cid name
takes
create tab l e s tudent (s id i n t e g e r pr imary key ,name varchar2 (30) not n u l l )
c reate tab l e course (c id i n t e g e r pr imary key ,name varchar2 (30) not n u l l ,s i d i n t e g e r re ferences student ( s id ) )
An alternative is to use the three table approach in many-to-many
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 35 / 40
One-to-One
A student can only take one course, a course can only have onestudent
Student
sid name
Course
cid name
takes
create tab l e studentcourse (s id i n t e g e r pr imary key ,c id i n t e g e r unique not n u l l ,studentname varchar2 (30) not n u l l ,coursename varchar2 (30) not n u l l )
Ugly!
Use the two table approach listed from the one-to-many example
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 36 / 40
ERD to Tables the Hole Story
1 Regular entity types2 Weak entity types3 Binary 1:1 relationship types4 Binary 1:n relationship types5 Binary m:n relationship types6 n-ary relationship types7 Multi-valued attributes
This mapping can be automated
Is supported by various ER-modeling tools
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 37 / 40
Outline
1 Introduction
2 Basic StructuresEntity SetsRelationship SetsAttributesWeak Entities
3 Bad and Ugly Designs
4 ERD to Tables
5 Summary
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 38 / 40
Guidelines
Good AdviseUse the same ERD notation through out the project
Each entity set should have a primary key
Be careful not to repeat information
Use composite attributes where appropriate
Watch out for n-ary relationships (they bite!)
Watch of for cardinalities and participation constraintsUse a tool, that can map ERDs to tables!
An tables to ERD
Stress TestPrint ERD on single page (no attributes)
Explain the diagram to your neighbor!
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 39 / 40