CS 461, Database Systems Name: Midterm Exam SOLUTION Drexel email: 1 25 2 25 3 30 Total 80 I certify that this exam is entirely my own work. Signature Date Instructions: This is an open book, open notes exam. Use of electronic devices is not permitted. You have 1 hour to answer all questions. Show your work when appropriate. Good luck! 1
Problem 1 (25 points) Business rules to ER diagrams (a) (10 points) Consider the business rules below that describe a database of books and authors. Draw an ER diagram that encodes these business rules. Clearly mark all key and participation constraints. An author is described by a name and a date of birth (dob). No two authors have the same combination of name and dob. A book is described by an ISBN, a title and a year when it was written (year). No two books have the same ISBN number. A book is written by exactly one author. An author is only included in the database is she has authored at least one book. There is participation on AUTHORS (bold line), and key and participation on BOOKS (arrowhead, bold line). (b) (15 points) Consider the business rules below that describe a database of scientists, discoveries and awards. Draw an ER diagram that encodes these business rules. Clearly mark all key and participation constraints. A scientist is described by a name, a field of study, and a date of birth (dob). No two scientists have the same combination of name and dob. A discovery has a name. No two discoveries have the same name. An award has a name, which uniquely identifies it. Scientists make discoveries. A discovery is made by one or several scientists. A scientist who made no discoveries is not tracked in our database. Scientists receive awards. A scientist may receive the same award more than once in different years. All years in which a scientist received a particular award must be recorded. There is participation on SCIENTISTS and DISCOVERIES w.r.t. make (bold lines). 2
Problem 2 (25 points) ER diagrams to SQL Do not create cycles when you establish foreign keys, i.e., you should not have a foreign key from table A to table B and also a foreign key from table B to table A in your implementation. (a) (15 points) Consider an ER diagram below. Write SQL statements (create table) that implement the constraints specified by this ER diagram. Create as many tables as is required. Briefly explain which constraints are captured in your relational implementation, and in what way. If a constraint cannot be implemented, state that explicitly in your description. You will not receive full credit without a proper description. The line from CHEFS to work_at is bold and has an arrowhead. The line CHEFS to own is not bold and has an arrowhead. The lines from RESTAURANTS to own and work_at are not bold and do not have arrowheads. create table Restaurants ( name varchar(64), city varchar(64), primary key (name, city) create table Chefs ( ssn char(9) primary key, name varchar(64), work_at_name varchar(64) not null, work_at_city varchar(64) not null, own_name varchar(64), own_city varchar(64), foreign key (works_at_name, works_at_city) references Restaurants (name, city), foreign key (owns_name, owns_city) references Restaurants (name, city) This implementation captures all constraints. Both key constraints on CHEFS are captured by representing the entity set CHEFS and the two relationship sets 3
in the same table with foreign keys (i.e., there is only one tuple per chef in this table, hence a chef cannot be associated with more than one restaurant through either ownership or work). Participation on CHEFS in work_at is represented by making work_at_name and work_at_city not null. (b) (10 points) Consider an ER diagram below. Write SQL statements (create table) that implement the constraints specified by this ER diagram. Create as many tables as is required. Briefly explain which constraints are captured in your relational implementation, and in what way. If a constraint cannot be implemented, state that explicitly in your description. You will not receive full credit without a proper description. The line from SATELLITE to orbits is bold and has an arrowhead; the line from PLANET to orbits is not bold and has an arrowhead. Option 1: create table Satellites_Orbit_Planets ( sat_name varchar(64) unique, sat_mass integer, planet_name varchar(64) primary key, planet_mass integer This implementation captures all constraints. Key constraints on both entity sets are captured because there is only 1 tuple in this relation per planet (because planet_name is the primary key) and per satellite (because sat_name is unique). Participation on Satellites is enforced because there cannot be a tuple in this relation in which sat_name is specified by planet_name is not (since planet_name is not null). Note that there is no participation on planets, which is correct. A planet without a satellite can exist in our database, for this planet the value of sat_name will be null. A shortcoming of this implementation is that there is an opportunity to specify sat_mass without specifying sat_name. However, this is subtle and this solution is acceptable for full credit. Option 2: 4
create table Planets ( name varchar(64) primary key, mass integer create table Satellites_Orbit_Planets ( name varchar(64) primary key, mass integer, planet_name varchar(64) not null unique, foreign key (planet_name) references Planets(name) This implementation captures all constraints. Key and participation on SATELLITES is captured by representing both the entity set SATELLITES and the relationship set in the table Satellite, with satellite name as the primary key. Key constraint on PLANETS is represented by making planet_name unique in Satellites. This way a particular planet participated in the relationship set at most once. Note that this solution does not suffer from the shortcoming of the implementation in Option 1. 5
Problem 3 (30 points) Relational algebra and SQL Olympics (year, host, kind) Medals (country, sport, year, event, medal) Olympics (year, host, kind) year host kind 2012 UK Summer 2010 Canada Winter 2008 China Summer 2006 Italy Winter 2004 Greece Summer 2002 USA Winter Medals (country, sport, year, event, medal) country sport *year event medal USA swimming 2012 100m freestyle gold USA swimming 2012 100m butterfly gold USA swimming 2012 200m butterfly silver Canada figure skating 2010 ice dancing gold Canada figure skating 2010 ladies singles bronze China figure skating 2010 pair skating gold China swimming 2008 400m freestyle silver USA swimming 2008 200m freestyle gold Brazil swimming 2008 50m freestlyle gold USA figure skating 2002 men s singles bronze USA figure skating 2002 ladies singles gold China figure skating 2002 pair skating bronze Russia figure skating 2002 men s singles gold Russia figure skating 2002 ladies singles silver 6
(a) (10 points) Write a SQL query that lists years in which the host country won a gold medal and a silver medal at an Olympic event. select o.year from Medals m1, Medals m2, Olympics o where m1.year = o.year and m1.country = o.host and m1.medal = 'gold' and m2.year = o.year and m2.country = o.host and m2.medal = 'silver' (b) (10 points) Write two equivalent relational algebra expressions that list sports in which the US won a medal in a Summer Olympics. Any two of these are sufficient, other solutions are also possible. π sport ((σ country='usa' Medals) >< (σ kind='summer' Olympics)) π sport (σ country='usa' (Medals >< (σ kind='summer' Olympics)) π sport (σ country='usa' kind='summer' (Medals >< Olympics)) (c) (10 points) Write a SQL query and an equivalent relational algebra expression that lists names of countries that won a medal at a Summer Olympics but that never won a medal at a Winter Olympics. List each country name only once. select distinct m.country from Medals m, Olympics o where m.year = o.year and o.kind = 'Summer' EXCEPT select m.country from Medals m, Olympics o where m.year = o.year and o.kind = 'Winter' Note that distinct is only necessary in the first part of the query. But if it s there in both parts, the query is correct and received full credit. There are several ways to write the relational algebra expression, one option is below. π country ((σ kind='summer' Olympics) >< Medals) π country ((σ kind='w int er' Olympics) >< Medals) 7