Base de Datos
Base de Datos
Base de Datos
The exercises marked with * will be discussed in the exercise session. You can solve the other
exercises as practice, ask questions about them in the session, and hand them in for feedback.
All exercises may be relevant for the exam.
Ask Ingo (ingo.mueller@inf.ethz.ch) for feedback on this week’s exercise sheet or give it
to the TA of your session (preferably stapled and with your e-mail address).
person 1 1
passport number has e-mail address
name
is_a
father mother
man woman
1 1
isSon isDaughter
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 1:
Entity-Relationship Model, February 27/March 3, 2017 Legi-Nr:
For each of the following groups of statements, pick the one that coincides best with the
model.
Explanation: The e-mail entity is the weak entity of the has relationship, so it must
participate in this relationship. Furthermore, the cardinalities restrict it to participate
at most once (as with any weak entity).
Explanation: Each entity has exactly one value for each attribute.
Explanation: Entities may or may not participate in relationships, so a man may not
have a father. Furthermore, the same man entity can participate several times as son
in the isSon relationship as long as it is with a different father and a different mother.
This is because the 1:1:N cardinality of the isSon relationship only limits the number of
man entities in the role of father to 1, given a man entity in the son role and a woman
entity.
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 1:
Entity-Relationship Model, February 27/March 3, 2017 Legi-Nr:
For each of the following statements, say whether it is true or false in the model.
√
5. Each passport number belongs to at most one person. True
False
Explanation: Passport number is the key of the person entity, so each passport number
is unique and used only by one person.
√
6. There are no two persons with the same name.
True False
Explanation: Name is not a key of the person entity, so it’s values are not restricted
and can occur multiple times.
√
7. Each person who is not a man is a woman.
True False
√
8. Each person who is a man is not a woman.
True False
Explanation: In this ER model, a person can be both a man and a woman at the
same time. In general, an entity of the general entity type of a generalization may
also be an entity of multiple different specializations. In other words, the definition of
generalization in ER says that the set of entities of each special type (here: “man” and
“woman”) is a subset of the entities of the general type (here: “person”), but it does not
say anything about whether the subset of the special types overlap or not. (It also does
not require that each entity of the general type is an entity of one of the special types.)
√
9. Each woman is a person. True
False
√
10. A man can be his own father. True
False
Explanation: There is no constraint that prevents the same man entity to participate
both in the man role and in the son role in the isSon relationship.
√
11. If a woman has a father, she also has a mother. True
False
√
12. Each man is a father.
True False
Explanation: A man entity may or may not participate in the isSon relationship (in
either role).
√
13. Each father is a man. True
False
Explanation: Only man entities can take the father role of the isSon relationship.
√
14. Each son is a man. True
False
Explanation: Only man entities can take the son role of the isSon relationship.
√
15. Each woman has at least one daughter.
True False
Explanation: A woman entity may or may not participate in the isDaughter relation-
ship (in either role).
√
16. Each woman has at most one daughter.
True False
Explanation: The 1:1:N relationship allows a woman entity arbitrarily many partici-
pations in the mother role, for example each time with a different woman entity in the
daughter role (or with the same daughter, but a different father).
For the given cardinality in the form “F1 : F2 ”, give the equivalent cardinality in the min/max
notation by completing the following table.
1: 1 (0, 1) (0, 1)
1: N (0, ∗) (0, 1)
N : 1 (0, 1) (0, ∗)
N : M (0, ∗) (0, ∗)
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 1:
Entity-Relationship Model, February 27/March 3, 2017 Legi-Nr:
Explanation: This table makes it clear that the two notations have to be read “the opposite
way”. Let us look closely at what the “1” means in the case of “1 : N ”. In the notation of
the cardinalities, it means that there is one E1 for each entity E2 . We write the “1” on the
side of E1 (and not on the side of E2 ). This is because, in relationships with more than two
entities, a “1” means that there is one E1 for each set of the other entities (see discussion of
next question). In order to express the “1” in the min/max notation, we can only limit the
number of occurences of each entity in isolation. In the example of the exercise, we have to
say that E2 can occur at most once, which implies the fact that there can be at most one
E1 for that E2 . This is written as “(min2 , max2 ) = (0, 1)” on the side of E2 (since it is the
entity we put the limit on).
Solution: No, you cannot. Consider the seminar example from the lecture: the entity
types “student”, “professor”, and “topic” are in a N:1:1 relationship “supervise”. The “1”
for the topic entity type means that, given an entity of type professor and an entity of type
student, there is at most one entity of type “topic” (i.e., a student can only take one topic
with a professor). You may be tempted to express this as (0, 1) in the min/max notation,
which would mean that each topic can be supervised at most once. However, in the N:1:1
relationship “supervise”, each topic entity may participate several times—just not with the
same student. In the min/max notation, there is no way to epxress a N:1:1 relationship
exactly. At the same time, other situations may only be expressable using the min/max
notation, so we may need both depending on what we want to do.
A B
MA MB
MC
The table below shows entities of type A, B, and C respectively that are connected through
relationship R. For example, the first tuple hm, o, xi indicates that entity m of type A, entity o
of type B, and entity x of type C are connected through R.
We are now interested in the question which are valid sets of tuples for R with different
cardinalities MA , MB , and MC .
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 1:
Entity-Relationship Model, February 27/March 3, 2017 Legi-Nr:
For each assignment of cardinalities in the table, select a maximal subset of tuples that form
a valid set R. Select a tuple by adding a checkmark to the corresponding cell. If a tuple cannot
be in R, write down the number of the tuple with which it conflicts.
1 m o x X X X X
2 n o z X X X X
3 m q y X X X X
4 n p z X X 2 2
5 n o x X 1 1 1
6 m q z X X X 3
Explanation:
We fill the table column by column:
• MA = N , MB = M , MC = O: If we take two entities out of the three entity types A,
B, or C, there can be N tuples in R with these two entities. This means that any set
of tuples is allowed for R (but it must be a set), so we select all tuples.
• MA = 1, MB = N , MC = M : For any two entities of types B and C respectively, there
may only be a single entity of type A; and there are no other constraints. However,
entity o of type B and entity x of type C participate in R with entities m and n of
type A, so we can only select one of the two (we select tuple 1).
• MA = 1, MB = 1, MC = N : We have the same constraint on entities of type A as
before, so we can only select the tuples that we selected previously. Additionally, for
any two entities of types A and C respectively, there may only be a single entity of
type B. Since entity n of type A and entity z of type C participate in R with entities
o and p of type B, we can only select one of them (we select tuple 2).
• MA = 1, MB = 1, MC = 1: We have the same constraint on entities of types A and
B as before, so we can only select the tuples that we selected previously. Additionally,
for any two entities of types A and B respectively, there may only be a single entity of
type C. Since entity m of type A and entity q of type B participate in R with entities
y and z of type C, we can only select one of them (we select tuple 3).
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 1:
Entity-Relationship Model, February 27/March 3, 2017 Legi-Nr:
Solution:
Apartement
Copy Country
N 1
Located
Availablein Located
Availablein
1 N
House
Copy Copy
City
N 1
Located
Availablein Located
Availablein
1 Street
Copy N
2. Two teams play football against each other. A referee makes sure the rules are followed.
Solution:
Team
Home Away
M N
Plays
Referee
and customers may order the same part of different suppliers in the same order, but in this case,
they may have different (retail) prices. Customers and suppliers have a name, an address, a
phone number, and a customer/supplier number and they come from a certain nation, which in
turn is from a particular region (of the world). Parts have a brand, a size, and a retail price.
Solution:
id status date
address
1 N
name customer places order number
M
phone M
price
is from
quantity
name 1
nation consists of price
region 1
shipdate
is from
phone price
O N
M
M N
name supplier provides part size
address id id brand
Solution:
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 1:
Entity-Relationship Model, February 27/March 3, 2017 Legi-Nr:
1
Weak Entity
is-a
Attribute of N 1
Copy Available
Has Entity Name
Entity
Functionality
N Functionality
Name IsPartOfKey
Role
Role
Participate
Available Participate
Min
Min
Name M
Max
Max
Attribute of N 1
Copy Available
Has Relationship Name
Relationship
is-a
Relationship of
Copy
1 Weak Entity
Not expressed:
• Relationships of weak entity types can only be N:1 or 1:1. In particular, they are
binary.
• Relationships (in the miniworld) are weak entity types (in its model) and they need to
be connected to at least two entity types.
• For each relationship, the role names of the participating entity types are unique.
• An entity type can participate several times in the same relationship through different
roles.
7 Miniworld: Olympics
Olympic games happen in a certain year at a certain place. Each year, there is at most one
instance of Olympic games. In each discipline of an olympic game, there is exactly one gold
medalist, one silver medalist, and one bronze medalist. All these medalists are athletes. Give an
ER model for this mini world. Identify the keys and give the functionalities of all relationships.
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 1:
Entity-Relationship Model, February 27/March 3, 2017 Legi-Nr:
Solution:
Place
Discipline
Year Name
N
Game M won-gold
1
Athlete
won-silver won-bronze
Some remarks:
• Note that if the key of Game would based on year and place, the requirement that
there is at most one event per year would be violated.
• The way the medalists are modeled is by creating three ternary relationships for Gold,
Silver, and Bronze medals each (from which we pictured only one). While this solution
fulfills the requirements, it also allows for the same athlete to win three medals in the
same discipline. Can you find a more “elegant solution” without losing any constraints?
8 Miniworld: Travelling
Consider the following mini-world. A person has a name and an age. Cities have a name and
are located in a country. Every year, persons can form groups in order to travel together to a
city. A person may be part of the same or a different group in different years, but may be part
of at most one group in any given year. Furthermore, a group travels to the same or different
city in different years, but travels to exactly one city in any given year. Give an ER model for
this mini-world. Identify the keys and give the functionalities of all relationships (e.g. 1:1, N:1,
N:M).
Solution:
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 1:
Entity-Relationship Model, February 27/March 3, 2017 Legi-Nr:
Country
Name
1
Person Group
Age N 1 N Is-in
Belongs-to
N
M Travels-to Name
1
M Copy
City
Year
Note that Group, Year, and Country have a single attribute “name” that is their primary
key. This has been omitted from the figure for simplicity.
Also note that the model makes the following compromise: On the one hand side, it
limits the number of groups each person can belong to in a given year by making Year a
separate entity type and using functionalities for “belongs-to”. On the other side, it does
not model the fact that each group “travels to exactly one city in any given year”. We could
model “exactly one” with the min/max notation, but then the constraint above (a person
can belong to at most one group per year) could not be enforced anymore.
Solution:
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 1:
Entity-Relationship Model, February 27/March 3, 2017 Legi-Nr:
ReaderNr
ReturnDate Shelf
FamilyName Position
Firstname N M
Reader Borrows Copy CopyNr
City N
Birthdate
Available
Catname
1 1 ISBN
N M PubYear
Contains Category InCat Book
N
Title
N
Author
1 NumPages
Publisher Publishes
Pubname
Pubcity
Alternative solutions:
• Borrows as 1:N (a copy can be borrowed only once). Then we model “Borrows” as
“Borrows currently” instead of “Has borrowed at some point”. Both solutions are
correct interpreations of the text, but model something slightly different.