Databases EECS 484 HW1 soln
-
Upload
alsamixers -
Category
Documents
-
view
119 -
download
6
description
Transcript of Databases EECS 484 HW1 soln
![Page 1: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/1.jpg)
Homework 1 Solution
Discussion Session 3
![Page 2: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/2.jpg)
EECS 484
Question 1
2
Which of the following statements are true?
Consider the following ER Diagram, which associates actors and movies.
![Page 3: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/3.jpg)
EECS 484
Question 1
3
Which of the following statements are true?
a)An actor can star in only 1 movie.False. There is no key constraint between Actor and Stars
b)Each actor has to star in a movie.False. There is no participation constraint between Actor and Stars
Consider the following ER Diagram, which associates actors and movies.
![Page 4: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/4.jpg)
EECS 484
Question 1
4
Which of the following statements are true?
c) Every movie has an actor.True. There is a participation constraint between Movie and Stars
d) A movie can have no more than one actor with a particular name.True. In this diagram, a movie can only have 1 actor to begin with.
Consider the following ER Diagram, which associates actors and movies.
![Page 5: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/5.jpg)
EECS 484
Question 1
5
Which of the following statements are true?
e) There can be multiple movies called ‘Inception’ in the year 2010.True. The pair (name,year) is not unique nor a primary key for the Movie entity.
f) Any two movies must have different names. False. The name of a movie is not unique nor a primary key
Consider the following ER Diagram, which associates actors and movies.
![Page 6: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/6.jpg)
EECS 484
Question 2(a)
• a) Can we create a “greatest hits” album containing most successful songs from past albums. Why or why not?
• There is a key constraint on Songs in Appears relationship. We cannot have a song that appears in multiple albums. Hence, we cannot create a new "greatest hits" album with songs already present in other albums. 6
![Page 7: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/7.jpg)
EECS 484
Question 2(b)• b) Suppose that you have
noticed that musicians often keep multiple addresses due to touring. Does this ER diagram allow musicians to have multiple addresses? Why or why not?
• Yes, a musician can be associated with multiple instances of the home relationship set, each of which includes an address and one or more phone numbers.
7
![Page 8: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/8.jpg)
EECS 484
Question 2(c)• c) Suppose that you want
to enforce the constraint that every musician must play an instrument. Does this ER diagram capture this constraint? If not, what do you need to do?
• You need to draw a bold line from the "Musicians" entity set to the "Plays" relationship set, which indicates a participation constraint. That is, every Musician entity must participate in at least one Plays relationship 8
![Page 9: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/9.jpg)
EECS 484 9
Question 2(d)
• d) Suppose Jay-Z and Justin Timberlake, two different musicians, wanted to produce a combined album together. Does this ER diagram allow us to represent this situation? Why or why not? If not, what must be changed?
• An album can only be produced by 1 Musician therefore a combined album would not be possible according to this diagram. If there was not a key constraint on Album to Produces, then it would be possible (change the bold arrow to just a bold line).
9
![Page 10: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/10.jpg)
EECS 484
Question 3
10
Your Great Uncle Rahul has hired you to improve the data management practices of his restaurant. Use an ER diagram to depict the following requirements.The restaurant employs a number of chefs. For each chef, you must keep track of the chef’s name, address, phone number, rating, and salary. (Do not assume that chef names are unique.)Each chef must be able to prepare one entrée at the minimum, and you must record the name of each entrée, as well as its price. Two entrées cannot have the same name. Each entrée contains several ingredients. The name of the ingredient, as well as the quantity required for that particular entrée, should be recorded. Every ingredient belongs to some entrée.Each entrée must be prepared by some chef and must contain some ingredient.Entrées are ordered by customers. Each customer has a unique ID. A record is kept of each customer’s name, address, total bill, and phone number. You must also record the time and date when each entrée was ordered.
![Page 11: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/11.jpg)
EECS 484
Question 3 Solution(1)
![Page 12: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/12.jpg)
EECS 484
Question 3 Solution(2)
12
Alternate solution: Aggregation with ‘Orders’ separate entity and total bill as an attribute
![Page 13: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/13.jpg)
EECS 484
Question 4
13
Consider a relation R with attributes A, B, and C. Suppose that you are given the following relation:
Which of the following attribute sets can you conclude are not keys for R? Briefly, justify your answer.
A; B; C; BC; and AC
A B C
1 20 x
2 30 y
3 15 z
4 45 w
5 20 z
![Page 14: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/14.jpg)
EECS 484
Question 4 Solution
14
Key: Minimal subset of attributes such that they are unique identifier for a tuple in all possible instances.
A candidate key satisfies the following:1) Two distinct tuples in any legal instance cannot have identical values in all fields of a key. 2) No subset of the set of fields in a key is a unique identifier for a tuple. In this case, we know B and C are not unique. Also a subset of AC, ‘A’, is a unique identifier. Therefore B,C,and AC are not keys.
A B C
1 20 x
2 30 y
3 15 z
4 45 w
5 20 z
![Page 15: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/15.jpg)
EECS 484
Question 5
15
Consider the following ER Diagram, which associates managers and employees.
Describe two reasonable translations to relational tables. For each translation, provide the appropriate DDL (i.e., CREATE TABLE) statements.
![Page 16: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/16.jpg)
EECS 484
Question 5 Solution
16
Translation 1: Manager(name, salary); Employee(id, salary); Manages(manager_name,employee_id)
CREATE TABLE Manager (name CHAR(20), salary INTEGER, PRIMARY KEY(name));
CREATE TABLE Employee (id INTEGER, salary INTEGER, PRIMARY KEY(id));
CREATE TABLE Manages(employee_id INTEGER, manager_name CHAR(20) NOT NULL, PRIMARY KEY(employee_id), FOREIGN KEY(employee_id) REFERENCES Employee, FOREIGN KEY(manager_name) REFERENCES Manager);
![Page 17: Databases EECS 484 HW1 soln](https://reader036.fdocuments.us/reader036/viewer/2022081501/55cf9bee550346d033a7e6ad/html5/thumbnails/17.jpg)
EECS 484
Question 5 Solution
17
Translation 2: Manager(name, salary); Manages_Employee(employee_id, salary, manager_name)
An assertion must be made to enforce the participation constraint, he/she should get full credit
CREATE TABLE Manager (name CHAR(20), salary INTEGER, PRIMARY KEY(name));
CREATE TABLE Manages_Employee(employee_id INTEGER, manager_name CHAR(20) NOT NULL, salary INTEGER, PRIMARY KEY(employee_id), FOREIGN KEY(manager_name) REFERNCES Manager);