Docsity

Database Design and Implementation - Homework 1 | CMPSCI 645, Assignments of Principles of Database Management

Toggle sidebar
bg1
CMPSCI 645 -- Homework 1
Due February 19, before class
Exercise [8 points] Relational Algebra
(a) Please express Division using the five basic relational operators. As with the example in class, let us
denote division by A/B where A = {x,y} and B={y}. For simplicity, assume that x, y are two attributes.
(b) The Semijoin of relations R and S, denoted R < S, is the s et of tuples of R(a1, a2, …, an) that agree with
at least one tuple of S on all attributes that are common to the schemas of R and S. Give an expression of the
relational algebra that is equivalent to R < S.
Solution:
(a)
ρ
(T, Ļ€x(A) Ɨ B)
ρ
(U, T – A)
ρ
(V, πx(U))
Ļ€x(A) - V
(b) A semijoin is equivalent to a natural join followed by a projection:
Ļ€ a1, a2, …, an (R S)
Exercise 2 [16 points] Queries in Relational Algebra
Consider the schema below.
student(snum:integer, sname:char(30), major:char(25), standing:char(2), age:integer)
faculty(fid:integer, fname:char(30), deptid:integer)
class(name:char(40), meets_at:char(20), room:char(10), fid:integer | fid REFS Faculty.fid)
enrolled(snum:integer, cname:char(40) | snum REFS student.snum, cname REFS class.name)
Write the following queries in relational algebra:
Q1) Retrieve the snum of students who have taken two different classes. Please only return distinct values
of snum.
Q2) Retrieve the snum and sname of students who have taken a class from ā€˜Ivana Teach’ or ā€˜Linda Davis’.
Q3) Retrieve the snum and sname of students who have taken classes from both ā€˜Ivana Teach’ and ā€˜Linda
Davis’.
Q4) Retrieve the snum and sname of students who have taken all classes taught by ā€˜Linda Davis’.
Solution:
(a)
ρ(R1, enrolled)
ρ(R2, enrolled)
Ļ€R1.snum(σR1.snum=R2.snum ∧ R1.cname≠R2.cnam e(R1 Ɨ R2))
(b)
Ļ€snum,sname((σfname=’Ivana Teac h’ ∨ fname =’Linda Davis’ faculty)  class  enrolled  student)
(c)
ρ(R1, Ļ€snum,sname((σfname =’Ivana Teach’ faculty)  class   enrolled)))
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Database Design and Implementation - Homework 1 | CMPSCI 645 and more Assignments Principles of Database Management in PDF only on Docsity!

CMPSCI 645 -- Homework 1

Due February 19, before class

Exercise [8 points] Relational Algebra

(a) Please express Division using the five basic relational operators. As with the example in class, let us denote division by A/B where A = {x,y} and B={y}. For simplicity, assume that x, y are two attributes. (b) The Semijoin of relations R and S, denoted R < S, is the set of tuples of R(a 1 , a 2 , …, an) that agree with at least one tuple of S on all attributes that are common to the schemas of R and S. Give an expression of the relational algebra that is equivalent to R < S. Solution: (a) ρ( T , Ļ€ x ( A ) Ɨ B ) ρ( U , T – A ) ρ( V , Ļ€ x ( U )) Ļ€ x ( A ) - V (b) A semijoin is equivalent to a natural join followed by a projection:

Ļ€ a 1 , a 2 , …, an (R S)

Exercise 2 [16 points] Queries in Relational Algebra

Consider the schema below. student ( snum :integer, sname :char(30), major :char(25), standing :char(2), age :integer) faculty ( fid :integer, fname :char(30), deptid :integer) class ( name :char(40), meets_at :char(20), room :char(10), fid :integer | fid REFS Faculty. fid ) enrolled ( snum :integer, cname :char(40) | snum REFS student. snum , cname REFS class. name ) Write the following queries in relational algebra: Q1) Retrieve the snum of students who have taken two different classes. Please only return distinct values of snum. Q2) Retrieve the snum and sname of students who have taken a class from ā€˜Ivana Teach’ or ā€˜Linda Davis’. Q3) Retrieve the snum and sname of students who have taken classes from both ā€˜Ivana Teach’ and ā€˜Linda Davis’. Q4) Retrieve the snum and sname of students who have taken all classes taught by ā€˜Linda Davis’. Solution: (a) ρ(R1, enrolled) ρ(R2, enrolled) Ļ€R1.snum(σR1.snum=R2.snum ∧ R1.cname≠R2.cname(R1 Ɨ R2)) (b) Ļ€snum,sname((σfname=’Ivana Teach’ (^) ∨ fname =’Linda Davis’ faculty)  class  enrolled  student) (c) ρ(R1, Ļ€snum,sname((σfname=’Ivana Teach’ faculty)  class  enrolled)))

ρ(R2, Ļ€snum((σfname=’Linda Davis’ faculty)  class  enrolled))) Ļ€snum,sname((R1 ∩ R2)  student) (d) ρ(R, Ļ€cname((σfname=’Linda Davis’ faculty)  class))) Ļ€snum, sname((enrolled/R)  student)

Exercise 3 [24 points] SQL Queries

You will use PostgreSQL to execute queries on a sample dataset that consists of four tables conforming to the schema in Exercise 2. Both the dataset and instructions for connecting to the PostgreSQL server are available on the assignment web page. Please take the following steps to complete this exercise. Step 1: Place the dataset in an appropriate place in your home directory on the edlab machines. Step 2: Connect to the PostgreSQL server. Step 3: Inside PostgreSQL, change your work directory to where your dataset is placed and load the dataset into corresponding tables. E.g. yanlei=> \cd ā€˜path-of-dataset’ yanlei=> \copy student FROM student.txt with delimiter as ',' yanlei=> \copy faculty FROM faculty.txt with delimiter as ',' yanlei=> \copy class FROM class.txt with delimiter as ',' yanlei=> \copy enrolled FROM enrolled.txt with delimiter as ',' You can use a SELECT query to check the content of each table. E.g., yanlei=> select * from student; Step 4: Now you are ready to submit SQL queries to retrieve the required information. Submission : Please create a directory ā€˜hw1’ under your home directory on the edlab machines. For each query below, store in the ā€˜hw1’ directory a valid SQL query, named ā€œQ1.sqlā€, ā€œQ2.sqlā€, …, and ā€œQ6.sqlā€. Write SQL expressions for each of the following queries and execute them: Q1) Retrieve the snum of students who have taken two different classes. Please only return distinct values of snum. Q2) Retrieve the snum and sname of students who have taken a class from ā€˜Ivana Teach’ or ā€˜Linda Davis’. Q3) Retrieve the snum and sname of students who have taken classes from both ā€˜Ivana Teach’ and ā€˜Linda Davis’. Q4) Retrieve the snum and sname of students who have taken all classes taught by ā€˜Linda Davis’. Q5) Find the age of the youngest student who has taken ā€˜Database Systems’. Q6) Find the name and age of the youngest student who has taken ā€˜Database Systems’. (Hint: For this query, you may consider using a derived table; if it is not supported by a particular DBMS, then create a view.)

-----------+-------------------------------- 112348546 | Joseph Thompson 115987938 | Christopher Garcia 322654189 | Lisa Walker 552455318 | Ana Lopez (4 rows) Q4) Retrieve the snum and sname of students who have taken all classes taught by ā€˜Linda Davis’. select S.snum, S.sname from student S where not exists ( select * from class C, faculty F where C.fid = F.fid and F.fname = 'Linda Davis' and not exists ( select * from enrolled E where E.snum = S.snum and E.cname = C.name ) ) snum | sname -----------+-------------------------------- 567354612 | Karen Scott (1 row) Q5) Find the age of the youngest student who has taken ā€˜Database Systems’. select min(S.age) from student S, enrolled E where S.snum = E.snum and E.cname = 'Database Systems'; min


17 (1 row) Q6) Find the name and age of the youngest student who has taken ā€˜Database Systems’. (Hint: For this query, you may consider using a derived table; if it is not supported by a particular DBMS, then create a view.) create view dblovers(snum, sname, age) as select distinct S.snum, S.sname, S.age from student S, enrolled E where S.snum = E.snum and E.cname = 'Database Systems'; select snum, sname from dblovers where age = (select min(age) from dblovers); snum | sname -----------+-------------------------------- 322654189 | Lisa Walker (1 row)

Exercise 4 [6 points] Language Theory

Can the following queries be expressed using conjunctive queries? If your answer is yes, write the conjunctive queries. If your answer is no, explain why. (a) find students who have taken ā€˜Database Systems’ but not ā€˜Operating Systems’ (b) find the age of the youngest student who has taken ā€˜Database Systems’ Solution: (a) No, because the query is not monotonic. As we know from the lecture, all conjunctive queries must be monotonic. (b) No, for the same reason as above.

(c) [5 points] Use your ER diagram to derive a 3NF schema for Joe’s database. You may want to introduce different or additional attributes (e.g., id fields) than those Joe chose to use in his initial schema. Please make primary key and foreign key constraints clear. Solution: Candidate { cid, name, dgender, bday, affliatedwith REFS Party.pid } Party { pid, name, address, money } Issue { iid, name, description } Supports { cid REFS Candidate.cid, iid REFS Issue.iid } RelatedIssues { cause REFS Issue.iid, effect REFS Issue.iid } You can also write CREATE TABLE statements. (d) [3 points] Suggest a set of non-trivial functional dependencies for this database from Joe’s requirements given above. Solution

  1. Candidate.cid → Candidate.name, Candidate.gender, Candidate.bday, Candidate.affliatedwith
  2. Party.pid → Party.name, Party.address, Party.money
  3. Issue.iid → Issue.name, Issue.description The following are trivial dependencies but needed for BCNF analysis:
  4. Supports.cid, Supports.iid → Supports.cid, Supports.iid
  1. cause, effect → cause, effect (e) [5 points] Use your functional dependencies to decompose Joe’s initial design of the database schema into BCNF. Solution: R1: Candidate ( candid, candname, candgender, candbday, partyname, partyaddress, partymoney ) R2: CandidateIssue ( issueid, supportingcandid, issuename, issuedescription ) R3: RelatedIssues ( cause-id, effect-id ) R1 is not in BCNF due to the following functional dependencies:
  2. Candidate.cid → Candidate.name, Candidate.gender, Candidate.bday, Candidate.affliatedwith
  3. Party.pid → Party.name, Party.address, Party.money Use FD2 to decompose R1 into: R11: (candid, candname, candgender, candbday, pid) R12: (pid, partyname, partyaddress, partymoney) Both R11 and R12 are now in BCNF. R2 is not in BCNF due to the following FD:
  4. Issue.iid → Issue.name, Issue.description Use FD3 to comcompose R2: R21: (issueid, supportingcandid) R22: (issueid, issuename, issuedescription) Both R21 and R22 are in BCNF. R3 is already in BCNF. (f) [2 points] Is the result of decomposition equivalent to the schema that you obtained from the ER diagram? If not, in what aspect do they differ? Yes (g) [3 points] Do both schemas address the three problems you listed above? Do they both preserve all of the functional dependencies you gave? Yes. Yes (h) [10 points] Given the JoeVotes database that you designed above, we now want to support the following query: Among all issues that directly or indirectly contribute to ā€œeducation budgetā€, find the one that is supported by the largest number of candidates. Please write a SQL statement for the above query. Hints: You can use the WITH construct in SQL to create temporary relations (either recursive or not) to be used in the final query. The syntax of WITH is the following: