





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






(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:
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)
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)
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