Database Design with ER Models
Database Design with ER Models
Table: Bookings
Table: Courses courseCode day timeslot room
Table: Rooms
courseCode name TDA357 Tuesday 0 GD
room seats
TDA357 Databases TDA357 Tuesday 1 GD
GD 236
ERE033 Reglerteknik ERE033 Tuesday 0 HB4
HB4 224
ERE033 Friday 0 GD
credits
Courses(name, credits)
name Course
credits
Courses(name, credits)
name Course
Multiple entities
• Separate entities are (for now) translated independently
name name
Courses(name, credits)
Programs(name)
The rest of this and tomorrows lecture
• I will present lots of features of ER-diagrams
• For each feature I will state something from a domain description (an
informal description of the database) and …
• … show how to model it in ER-diagrams
• … show how it is translated into a relational schema
• … say something about how to identify the feature in domains
• If there is time left, I will spend it demonstrating some more examples
and common mistakes
Courses(name, credits)
A simple relationship Programs(name)
name name
Courses(name, credits)
name name
Programs(name)
CourseIn(course,program)
credits Course CourseIn Program
course -> Courses.name
program -> Programs.name
Note that the credits attribute is not here, only the key
Naming and relationships
• During translation you need to decide a few names in the schema
• You can use the same name for attributes everywhere, but this is
sometimes confusing (like having an column called name in courses,
that is actually the name of a teacher) and sometimes does not work
• I like using the (lowercase) names of the connected entities:
name name
CourseIn(course,program)
course -> Courses.name
program -> Programs.name
Compound keys and relationships
• Remember: Always include the whole key of both the related relations!
place idnr
Databases TIDAL 2
credits Course CourseIn Program Databases TKIEK 3
Webapp TIDAL 2
Courses(name, credits)
Programs(name) When I add a
CourseIn(course, program, year) (course,program)-pair
course -> Courses.name to CourseIn, I also
program -> Programs.name
have to specify a year
Identify attributes on relationships in domains
• Things like "X may have a z in/for/at a Y" where X and Y are entities
and z is an attribute typically signify that z is an attribute of a relation
• Example: "Teachers can be assigned roles in courses"
(role is an attribute of a relationship between teachers and courses,
unless roles should be an entity…)
Multiplicity
• The relationships we have seen so far are called many-to-many
• E.g. A course can belong to many programs and programs have many courses
• What if I wanted to model something like "Each course has a single teacher" or
"Some of the teachers have an office"?
• These examples describe relationships, but they are not many-to-many
Many-to-exactly-one
• The Teaches relationship models "each course has a single teacher"
• If the arrow was in the other direction it would model "each teacher
has a single course"
credits tname
Teachers(tname)
Courses(name, credits, teacher)
teacher -> Teachers.tname
Reference constraint
Identify many-to-exactly-one in domains
• Anything like "Every X has a Y" (where Y is another entity) are typically
many-to-exactly-ones
• It's common that the language is ambiguous, e.g. "Xs have Ys" can
mean that each X can have multiple Ys, or that they each have one.
• If you want to add an attribute, but that attribute is more accurately
modelled as an entity, you should use many-to-one relationships
Many-to-at-most-one
• This diagram expresses "Some teachers have an office", or "A teacher
may have an office" or equivalent
• Also called many-to-one-or-zero
name number
name
Teachers(name) Change the key!
number
Offices(number)
TeacherOffice(teacher,office)
Teacher TeacherOffice Office
teacher -> Teachers.name
office -> Offices.number
Translating many-to-at-most-one: Null-approach
name Exactly one number Offices(number)
Teachers(name, office)
Teacher TeacherOffice Office office -> Offices.number
Offices(number)
Teacher TeacherOffice Office Teachers(name, office (or null))
office -> Offices.number
When can the Null approach be used?
The null approach can only be used under the following conditions:
• The "at-most-one"-side does not have a compound key
• The relationship does not have any attributes
• You are morally OK with having null values in your database
Courses(code, cname)
Teachers(tname)
Rooms(rname)
Lectures(course,teacher,room)
course -> Courses.code
teacher -> Teachers.tname
room -> Rooms.rname
A better example of a multiway relationship
• "Teachers can be assigned any of a list of roles (e.g. examiner, course
responsible, assistant) for any course."
• Here, roles should be an entity (the concept of examiner exists even if
there are currently no courses)
• Ambiguity: Can a teacher have roles on multiple courses?
• Reasonable assumption: yes (from our knowledge of the domain)
• Ambiguity: Can a teacher have multiple roles in the same course? (does
"any of" mean "any one of" or "any subset of"?)
• Let's assume a teacher can only have one role (and there is a special
role for being both examiner and course responsible for instance)
Key ensures we can associate any
Multiway relationship number of teachers with any number
of courses, and for each association
we have to select a valid role
rname
Courses(code, cname)
Role
tname
Teachers(idnr,tname)
code
Roles(rname)
CourseRole(course,teacher,role)
Course CourseRole Teacher
course -> Courses.code
teacher -> Teachers.idnr
cname idnr
role -> Roles.rname
Courses(code, cname)
Role
tname
Teachers(idnr,tname)
code
Roles(rname)
CourseRole(course,teacher,role)
Course CourseRole Teacher
course -> Courses.code
teacher -> Teachers.idnr
cname idnr
role -> Roles.rname
Role
code tname
cname idnr
Confusion conclusion
• Multiway relationships that are many-to-many-to-many and
many-to-many-to-one seem to make sense, other combinations are more
difficult to interpret
• There are even weirder examples like all connections having pointed arrow
• Some that are obviously the same as having two separate relationships, like
many-to-exactly-one-to-exactly-one
Identifying multiway relationships in domains
• Something like "Xs have Ys in Zs" where X, Y and Z are all entities
• Ambiguity: "Xs have Ys and Zs" usually mean two separate
relationships (X-Y and X-Z) but sometimes a multiway relationship
• Attributes on relationships should be replaced by multiway
relationships (and an entity) when the values are chosen from a list
(like roles in the earlier example)
• "Teachers have roles in courses" (assuming role is an entity not an
attribute), "A person must have a contract for each project they are
involved in" (assuming persons, contracts and projects are entities)
• Identifying multiplicity of the various connections is often difficult
New problem!
• Model: "Most employees have another employee as their boss."
• Being boss of someone is a relationship
• So this is a relationship between employees and employees
idnr ename
Employee
Arrow indicates at most one boss
Any resemblance between diagram per employee
and Gary the snail is coincidental
BossOf
Translating self relationships
… is done exactly like other relationships!
• Here using the null-approach (add the key from the at-most-one side
as an extra attribute on the many side, and make it nullable):
idnr ename Employees(idnr,ename, boss (or null))
boss -> Employees.idnr
Employee
Self reference!
User
Users(uname)
blocking blocked Blocked(blocking,blocked)
blocking -> Users.uname
Blocked blocked -> Users.uname
Limitations of self-relationships
• Some things that cannot be expressed in ER-diagrams:
• Can a value be related to itself? (I'm my own boss? I block myself?)
• Can a there be cycles (I'm the boss of my bosses boss? I'm blocked by a
person I block?)
• Is the relationship symmetric, e.g. for a Sibling-relationship:
If a is a sibling of b, then b must also be a sibling of a
• These can be expressed in side-notes/comments, but may be difficult to
implement in SQL
Identifying self relationships in domain
• Anything on the form "X has … to [an]other X"
• If you want to model any kind of tree-structure (many-to-at-most-one) or
graph structures (many-to-many) on values of an entity
Start of lecture 5 – The story so far
Domain description ER-diagram schema SQL code
ER-diagrams:
attribute
Modelling Translation
Entity
uname
N to N
Relationships
User
(many-to-many,
N to 1
many-to-one, blocking blocked
many-to-at-most-one)
Blocked
N to 0/1
Self-relationships
Weak entities
• Model this: "Each company has divisions, and each division has a name.
Two divisions in the same company cannot have the same name."
• Problem: What is the primary key of Divisions?
Too strong key
dname cname
Companies(cname)
Divisions(dname,company)
Division Of Company company -> Companies.cname
???
dname cname
Companies(cname)
Divisions(dname,company, company2)
company Division Of Company company2 -> Companies.cname
Missing reference for company
Weak entities
• To deal with this situation, we add a feature called Weak entities
• A weak entity cannot be identified only by its own attributes
• It requires support from at least one other entity
• The diagram below expresses that a division is identified by its name
along with the identity of the company it belongs to:
dname cname
Dashed underlining
Division Of Company
(partial primary key)
dname cname
Companies(cname)
Divisions(dname,company)
Division Of Company
company -> Companies.cname
Identify weak entities in domain descriptions
• Things like "Player numbers are unique within teams" or "players can
have the same number assuming they are on different teams"
• If you notice that the attributes you have determined for an entity are not
sufficient to identify members, perhaps it should be a weak entity
• If you want a key attribute on a relationship, you can turn the relationship
into a weak entity supported by both the related entities (a bit of a hack)
Courses(cname)
rname time cname
Rooms(rname)
Room In Lecture Of Course
Lectures(course,room,time)
course -> Courses.cname
room -> Rooms.rname
Inheritance and subentities
• Next up: What if I have something like "Some companies are
corporations. Each corporations has a year of incorporation."
• Adding a year attribute to Company does not work, that implies all
companies have a year of incorporation, and it's impossible to tell
which companies are corporations.
name
Company corpyear
Why is this bad?
• In this design some companies have corporation years.
• Problem: Look at the CorpYear entity in isolation. What does it
contain? All years? All years where at least one corporation has been
formed (and no other years can be added)?
Company name
Points at superentity
No relationship name
ISA
(always labeled ISA)
Corporation year
No key attributes
Translating ISA-relationships: ER-approach
• Make a new relation that only has the primary key of the superentity and the
extra attributes added by the subentity, and a reference to the superentity
Corporation year
The reference means each
corporation also has an address
etc. - it really IS A company
Translating ISA-relationships: Null approach
• Just add the extra attribute to the superentity, but nullable
address Company name
ISA
Corporation year
ISA
PhdCourse Courses(code)
ISA
Reference to Corporations
year Corporation
Persons(idnr)
Companies(name, address, year (or null))
BoardMember NULL BoardMembers(person,corp)
person -> Persons.idnr
corp -> ???
idnr Person
Putting Companies.name here would be bad
(means all companies can have board members)
Problems with the NULL-approach
The NULL approach can only be used if:
• The subentity has a single attribute
• The subentity has no relationships to other entities
• Includes not having its own subentitites
Corporation year
idnr
Translation of example in last slide
Employed Company name
ISA
Employee
Corporation year
idnr
All corporation names
Employees(idnr) are also company names
Companies(name)
Corporations(name, year)
name -> Companies.name
Employed(employee, company)
Any company name
employee -> Employees.idnr
company -> Companies.name can be used here
ISA relationships and primary keys
• Subentities can never have any key attributes of their own!
• Defies the concept of inheritance, if entity X is not identified by the same
attributes as entity Y, we can never claim "X is a Y", it is something else
• If you need additional identifying attributes, use weak entities
• In a way, weak entities are "subentities with extra identifying attributes"
Identifying ISA-relationships in domains
• When it makes sense to say "X is a Y", like "Corporation is a Company" or
"Employee is a Person" or "Car is a Vehicle"
• In domain texts, it may also be stated as variations of "some X have y",
where y is an attribute and not another entity (optional attributes)
• Sometimes when you want to model a subset of some entity even if they
don't have extra attributes
Mission Accomplished!
You now know (or have at least heard of) all features of ER required for task 2!
Practicalities of making ER-diagrams
• We recommend using Dia to make diagrams (http://dia-installer.de/)
• Make sure you set it to ER-mode
• Pro-tip: Double-click an entity/relation to
set its properties (e.g. weak etc) ER-mode
• Pro-tip: Double-click an attribute to set
primary key etc.
• Pro-tip: Double-click a line to change arrows, the curved arrow is
available under "more arrows"
• Pro-tip: There are no ISA-relationships in the ER mode, but you can use
merge/extract from flowchart (and just write ISA on them)
• Pro-tip: Always connect lines to the points of diamonds, not their center
I made a tutorial video explaining all of this and more!
Order of translation
• Always start with an entity whose translation does not depend on knowing the
keys of other entities
• Example: If two entities have a many-to-exactly-one relationship, always
start with the entity on the exactly-one side
• Example2: Translate superentities before you translate subentities
• Basically: Do translations in reverse order of how the arrows are pointing
• Do many-to-many relationships last
• If you find yourself writing a reference to a relation you have not yet
translated, you are doing things in the wrong order
A small domain example
"Make a database of companies in a corporate group. The companies
have employees, that are divided into divisions within each company.
Divisions in the same company cannot have the same names. Most
employees have a boss.
Some employees are given a managerial position with a special title.
Also make an inventory of the company cars, who has access to which
cars (only managers can have access to company cars) and for how
long they have had access."
An ER-diagram with almost everything
BossOf cname
dname
ssn boss
salary
ISA
sinceyear model
a1 A R B b2