An overview of ER-diagrams including entity sets, relationship sets, and attributes. The four attributes types are covered and cardinality constraints. Further partial or full participation is discussed.
Identifying Appropriate Test Statistics Involving Population Mean
ERD Guide to Entity Relationship Diagrams
1. Entity-Relationship Diagrams (ERD)
Kristian Torp
Department of Computer Science
Aalborg University
people.cs.aau.dk/˜torp
torp@cs.aau.dk
November 5, 2015
daisy.aau.dk
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 1 / 40
2. Outline
1 Introduction
2 Basic Structures
Entity Sets
Relationship Sets
Attributes
Weak 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
3. Learning Goals
Motivation
ER-diagrams are widely used
ER-diagrams are simple to learn
Much simpler than UML
An ER-diagram is a good communication tool
Goals
Create 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
4. Outline
1 Introduction
2 Basic Structures
Entity Sets
Relationship Sets
Attributes
Weak 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
6. Outline
1 Introduction
2 Basic Structures
Entity Sets
Relationship Sets
Attributes
Weak 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
7. Outline
1 Introduction
2 Basic Structures
Entity Sets
Relationship Sets
Attributes
Weak 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
8. Examples Entity Sets
computer
CID
Make
Model
order
OID Amount
Note
Nouns in the requirement specification
Content
Should 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
9. Entity vs. Entity Set
Entity ≈ object ≈ an instance ≈ row
Entity set ≈ class ≈ a schema ≈ table
Note
Often we just say entity when we mean entity set!
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 9 / 40
10. Outline
1 Introduction
2 Basic Structures
Entity Sets
Relationship Sets
Attributes
Weak 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
11. Examples Relationship Sets
order order linehas
car gas stationtanking
computer motherboard cpuhas in
Note
Verbs in the requirement specification
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 11 / 40
13. Quiz
computer motherboard cpuhas in
Question
How many motherboards can a computer have?
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
14. Quiz
computer motherboard cpuhas in
Question
How many motherboards can a computer have? many
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
15. Quiz
computer motherboard cpuhas in
Question
How many motherboards can a computer have? many
Does the ERD support multi CPU computers?
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
16. Quiz
computer motherboard cpuhas in
Question
How many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
17. Quiz
computer motherboard cpuhas in
Question
How many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML?
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
18. Quiz
computer motherboard cpuhas in
Question
How many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
19. Quiz
computer motherboard cpuhas in
Question
How many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
Question
How many times can you tank your car?
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
20. Quiz
computer motherboard cpuhas in
Question
How many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
Question
How many times can you tank your car? many times
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
21. Quiz
computer motherboard cpuhas in
Question
How many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
Question
How many times can you tank your car? many times
Association or aggregation from UML?
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 13 / 40
22. Quiz
computer motherboard cpuhas in
Question
How many motherboards can a computer have? many
Does the ERD support multi CPU computers? yes
Association or aggregation from UML? aggregation
car gas stationtanking
Question
How 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
24. Participation
Student CourseTakes
Partial Participation
Partial participation both entities
Student CourseTakes
Full participation
Full participation student
All students have to take at least one course
Partial participation course
A 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
25. Participation Quiz
computer motherboard cpuhas in
Question
Can the shop sell computers without motherboards?
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
26. Participation Quiz
computer motherboard cpuhas in
Question
Can the shop sell computers without motherboards? yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
27. Participation Quiz
computer motherboard cpuhas in
Question
Can the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs?
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
28. Participation Quiz
computer motherboard cpuhas in
Question
Can the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs? no
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
29. Participation Quiz
computer motherboard cpuhas in
Question
Can the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs? no
Can the shop sell a separate CPU?
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
30. Participation Quiz
computer motherboard cpuhas in
Question
Can the shop sell computers without motherboards? yes
Can the shop sell motherboards without CPUs? no
Can the shop sell a separate CPU? no
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
31. Participation Quiz
computer motherboard cpuhas in
Question
Can 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
Question
Do we always know which payment a loan is for?
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
32. Participation Quiz
computer motherboard cpuhas in
Question
Can 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
Question
Do we always know which payment a loan is for? yes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 15 / 40
33. Role
Self Reference
An entity set may be related to itself
Assume each teacher has a manager that is another teacher
teacher works for
manager
manages
Note
Can be read as (but never draw this)!
teacher teacherworks for
manager manages
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 16 / 40
34. n-Ary Relationships
employee topic
meeting
discuss
Comment
At a number of meetings, a number of employees discuss a number
of 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
35. Outline
1 Introduction
2 Basic Structures
Entity Sets
Relationship Sets
Attributes
Weak 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
37. Relationship Sets and Attributes
car
ID Make Model
gas station
ID address
tanking
DateTime
Question
Why the DateTime on the tanking relationship set?
The characteristics of the entity and relationship sets in the
requirement specification
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 20 / 40
38. Outline
1 Introduction
2 Basic Structures
Entity Sets
Relationship Sets
Attributes
Weak 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
39. Examples
order order linehas
Question
Are order line depending on order?
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 22 / 40
40. Examples
order order linehas
Question
Are order line depending on order? No not in this ERD
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 22 / 40
41. Examples
order order linehas
Question
Are order line depending on order? No not in this ERD
Update Diagram
Make 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
42. Summary: Basic Concepts
Concepts
Entity set
Strong
Weak (no key)
Relationship set
Cardinality: 1-1, 1-n, n-m
Participation: Partial or full
n-ary (be careful)
Attribute
Single
Composite
Derived
Multi-valued
Note
Quite simple to draw and use
Many alternative notations
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 23 / 40
43. Outline
1 Introduction
2 Basic Structures
Entity Sets
Relationship Sets
Attributes
Weak 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
44. Bad Design, One
Student
sid cid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 25 / 40
45. Bad Design, One
Student
sid cid name
Course
cid name
takes
Bad: Course ID (cid) on both entities. Fixed below!
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 25 / 40
46. 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
47. Bad Design, Two
Teacher
tid name room
Course
name ects
gives
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 26 / 40
48. Bad Design, Two
Teacher
tid name room
Course
name ects
gives
Bad: No primary key on the course entity. Fixed below!
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 26 / 40
49. 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
50. Bad Design, Three
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 27 / 40
51. Bad Design, Three
Student
sid name
Course
cid name
takes
Bad: Not connected to the course entity. Fixed below!
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 27 / 40
52. 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
53. Bad Design, Four
Student
sid name
Course
cid name
takes
sid cid
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 28 / 40
54. Bad Design, Four
Student
sid name
Course
cid name
takes
sid cid
Bad: IDs from entities on relationship. Fixed below!
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 28 / 40
55. 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
56. Bad Design, Five
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 29 / 40
57. Bad Design, Five
Student
sid name
Course
cid name
takes
Good — just checking
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 29 / 40
58. Bad Design, Five
Student
sid name
Course
cid name
takes
Good — just checking
Student
sid
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 29 / 40
59. 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
60. Bad Design, Six
Student
sid name
Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 30 / 40
61. Bad Design, Six
Student
sid name
Course
cid name
takes
Bad: This does not make sense with only weak entities. Fixed below!
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 30 / 40
62. 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
63. Bad Design, Seven
Student Course
cid name
takes
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 31 / 40
64. Bad Design, Seven
Student Course
cid name
takes
Bad: No attributes on student. Fixed below!
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 31 / 40
65. 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
66. Outline
1 Introduction
2 Basic Structures
Entity Sets
Relationship Sets
Attributes
Weak 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
67. Three Step Overview
1 Convert all entity sets to tables, a column for each attribute
2 Convert all relationship sets to tables, a column for the primary key of
the participation entity sets
3 A lot of details
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 33 / 40
68. Many-to-Many
A student can take many courses, a course can have many students.
Student
sid name
Course
cid name
takes
create table student (
sid integer primary key ,
name varchar2 (30) not n u l l )
create table takes (
sid integer
references student ( sid ) ,
cid integer
references course ( cid ) ,
primary key ( sid , cid ) )
create table course (
cid integer primary key ,
name varchar2 (30) not n u l l )
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 34 / 40
69. One-to-Many
A student can take many courses, a course can only have one student
Student
sid name
Course
cid name
takes
create table student (
sid integer primary key ,
name varchar2 (30) not n u l l )
create table course (
cid integer primary key ,
name varchar2 (30) not null ,
sid integer references student ( sid ) )
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
70. One-to-One
A student can only take one course, a course can only have one
student
Student
sid name
Course
cid name
takes
create table studentcourse (
sid integer primary key ,
cid integer unique not null ,
studentname varchar2 (30) not null ,
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
71. ERD to Tables the Hole Story
1 Regular entity types
2 Weak entity types
3 Binary 1:1 relationship types
4 Binary 1:n relationship types
5 Binary m:n relationship types
6 n-ary relationship types
7 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
72. Outline
1 Introduction
2 Basic Structures
Entity Sets
Relationship Sets
Attributes
Weak 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
73. Guidelines
Good Advise
Use 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 constraints
Use a tool, that can map ERDs to tables!
An tables to ERD
Stress Test
Print 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
74. Summary
Main Points
Widely used
Unfortunately there is not an ERD standard
Extended ER modeling
Supports inheritance
Supports categories
Many ER modeling tools
Alternatives to ERDs are UML or ODL
Kristian Torp (Aalborg University) Entity-Relationship Diagrams (ERD) November 5, 2015 40 / 40