Lecture 5
Lecture 5
5 credits)
Yuantao Fan
[email protected]
Halmstad University
Overview
• Schema
• Database design flow
• Entity-Relationship diagram
– Domain description
– Entities, multiple entities, weak entities
– Attributes of an entity, or a relationship
– Relationships
– Multiplicity
– Relations in ER-diagrams
• Examples
Designing Databases with Entity-Relationship (ER) Diagram
• Redundancy
– Duplicates in the number of seats for room D415
• Update anomaly & delete anomaly
– Change nn_seats in one row but not others
– nn_seats information is gone if all bookings of D415 is removed
Decomposing the table
Room
credits
Entities in boxes
Attributes in ellipses
From ER model to relational schema
credits
Multiple Entities
• Entities are named singular, while relations are in plural (in ER-diagrams)
cid tid
credits name
credits name
• Relationships in diamond-shpes
• Name describe the relationship
cid tid
credits name
Courses(cid, name, credits)
Teachers(tid, name)
TaughtBy(course, teacher)
course -> Courses.cid
teacher -> Teachers.tid
Compound keys and relationshipes
cid tid
credits name
Courses(cid, name, credits)
Teachers(tid, name)
TaughtBy(course, c_name, teacher)
course -> Courses.(cid, name)
teacher -> Teachers.tid
Attributes of relationships
credits name
Attributes of relationships
• Specifiy the year of responsible teacher of each course Courses(cid, name, credits)
Teachers(tid, name)
– Is year an attribute of course? teacher? or the relationship? TaughtBy(course, c_name, teacher, year)
• Note that relationship can never have key attributes course -> Courses.(cid, name)
– Always identified by the related entities teacher -> Teachers.tid
credits name
Degree of Relationships
• The degree of a
relationship tupe is the
number of participating
entity types
• The WORKS_FOR
relationship is of degree
two - binary
• The SUPPLY
relationship is of degree
three - ternary
Degree of Relationships
• The degree of a
relationship tupe is the
number of participating
entity types
• The WORKS_FOR
relationship is of degree
two - binary
• The SUPPLY
relationship is of degree
three - ternary
Page 83
Multiplicity
cid tid
credits name
Multiplicity
• ER-diagrams
At most one
Teache
oid Office rOffice Teacher tid
Multiplicity
Teache
oid Office rOffice Teacher tid
At most one
Teache
oid Office rOffice Teacher tid
Multiplicity
Teachers(tid)
Office(oid)
Teache
oid Office rOffice Teacher tid TeacherOffice(teacher, office)
office -> Office.oid
teacher -> Teachers.tid
At most one
Teache
oid Office rOffice Teacher tid
Multiplicity
Teachers(tid)
Office(oid)
Teache
oid Office rOffice Teacher tid TeacherOffice(teacher, office)
office -> Office.oid
teacher -> Teachers.tid
At most one
Teachers(tid)
Office(oid)
Teache TeacherOffice(teacher, office)
oid Office rOffice Teacher tid
office -> Office.oid
teacher -> Teachers.tid
Multiplicity
Teachers(tid)
Office(oid)
Teache
oid Office rOffice Teacher tid TeacherOffice(teacher, office)
office -> Office.oid
teacher -> Teachers.tid
At most one
Teachers(tid)
Office(oid)
Teache TeacherOffice(teacher, office (or null))
oid Office rOffice Teacher tid
office -> Office.oid
teacher -> Teachers.tid
Multiway relationship
rid
Courses(cid, credits)
Teachers(tid, name)
Role(rid)
Role TaughtBy(course, teacher, role)
cid tid
course -> Courses.cid
teacher -> Teachers.tid
roleà Roles.rid
Course CourseRole Teacher
Key pairs (course, teacher)
ensures assignment of any
number teachers with any
number of courses, for each
credits name
association we need to select a
valid role
Multiway relationship
rid
Courses(cid, credits)
Teachers(tid, name)
Role(rid)
Role TaughtBy(course, teacher, role)
cid tid
course -> Courses.cid
teacher -> Teachers.tid
roleà Roles.rid
Course CourseRole Teacher
At most one (teacher, role) pair
per course
credits name
Translating self relationships
ChatApp
User
Blocked
Translating self relationships
ChatApp
ChatAppUsers(id, name)
User
Blocked(blocking, blocked)
blocking -> ChatAppUsers.id
blocked -> ChatAppUsers.id
Blocked
Translating self relationships
ChatApp
ChatAppUsers(id, name)
User
Blocked(blocking, blocked)
blocking -> ChatAppUsers.id
blocked -> ChatAppUsers.id
Blocked
Translating self relationships
ChatApp
ChatAppUsers(id, name)
User
Blocked(blocking, blocked)
blocking -> ChatAppUsers.id
blocked -> ChatAppUsers.id
• A weak entity can be identified uniquely only by considering the primary key of
another (owner) entity
– In other words, it cannot be identified only by its own attributes
– The owner entity set and weak entity set must participate in a one to many relationship set
(one owner, many weak entities)
– Weak entity set must have total participation in this identifying relationship set
– Weak entities have only a partial key (dashed underline)
N 1
dname Department of Company name
Dashed underline
Weak entity Identifying relationship
(partial primary key)
(always many-to-one)
Weak Entities
• A weak entity can be identified uniquely only by considering the primary key of
another (owner) entity
• Identifying relationship is when the existence of a row in a child table depends on a
row in a parent table
– Make the foreign key part of the child's primary key
– logical relationship is that the child cannot exist without the parent
N 1
dname Department of Company name
Dashed underline
Weak entity Identifying relationship
(partial primary key)
(always many-to-one)
Example translating weak entities
Common Many-to-one relationship:
N 1
Companies(name)
dname Department of Company name department(dname, company)
company->companies.name
ER Diagram Examples
Page 64 Page 84