June 2, 2015R&G: Database Management Systems1 Introduction to Database Systems Key to As#1 Ex. 3 Instructor: Jiawei Han based on the notes by Raghu Ramakrishnan, UW- Madison Please observe the copyright of ©R&G. For classroom use only. Do not distribute!
June 2, 2015R&G: Database Management Systems 2 Question Consider relation database person(pname, street, city) works_for(pname, cname, salary) company(cname, city) manages (pname, manager) Write the following queries in relational algebra tuple relational calculus domain relational calculus SQL
June 2, 2015R&G: Database Management Systems 3 Find street and city of all employees who work for the Future Shop, live in Burnaby, and earn more than $40,000. Relational algebra street, city (( city =‘Burnaby’ Person) ( cname =‘FutureShop’ salary > Works_for)) or, street, city (( city =‘Burnaby’ Person) pname ( cname =‘FutureShop’ salary > Works_for)) Tuple relational calculus {T | P Person W Works_for (W.salary> W.cname = ‘FutureShop’ P.pname = W.pname P.city = ‘Burnaby’ T.street = P.street T.city = P.city)} Domain relational calculus { S, C | N N, S, C Person I,Y N, I, Y Works_for I = ‘FutureShop’ Y > C = ‘Burnaby’} SQL select P.street, P.city from Person P, Works_for W where P.pname = W.pname and W.cname = ‘FutureShop’ and W.salary > 40,000 and P.city = ‘Burnaby’
June 2, 2015R&G: Database Management Systems 4 Find the names and the companies they work for, for all the people who have a higher salary than their manager Relational algebra (T1, Works_for), (T2, Works_for), (M, Manages) (TM, M.pname, T2.salary ( M.manager = T2.pname ( M T2))) t1.pname, T1.cname ( TM.pname = T1.pname T1.salary > TM.salary ( TM T1)) Tuple relational calculus {T | W Works_for M Manages W2 Works_for (W.salary> W2.salary W.pname = M.pname M.manager = W2.pname T.pname = P.pname T.cname = W.cname)} Domain relational calculus { N, C | S N, C, S Works_for M N, M Manages C2, S2 M, C2, S2 Works_for S > S2} SQL select W.pname, W.cname from Works_for W, manages M, Works_for W2 where W.pname = M.pname and M.manager = W2.pname and W.salary > W2.salary
June 2, 2015R&G: Database Management Systems 5 Assume a company may located in several cities. Find all the companies located in every city in which the FutureShop is located. Relational algebra (T1, Works_for), (T2, Works_for), (M, Manages) (TM, M.pname, T2.salary ( M.manager = T2.pname ( Manages T2))) t1.pname, T1.cname ( TM.pname = T1.pname T1.salary > TM.salary ( Manages T1)) Tuple relational calculus {T | W Works_for M Manages W2 Works_for (W.salary> W2.salary W.pname = M.pname M.manager = W2.pname T.pname = P.pname T.cname = W.cname)} Domain relational calculus { N | N N,C Company M N, M Manages C2, S2 M, C2, S2 Works_for S > S2}
June 2, 2015R&G: Database Management Systems 6 Find all the companies located in every city in which the FutureShop is located. (SQL) select distinct a.cname from company a where a.cname <> 'FS' and not exists (select distinct c.cname from company c where c.cname <> 'FS' and c.city = a.city and exists (select d.city from company d where d.cname = 'FS' and not exists (select e.city from company e where e.cname <> 'FS' and e.cname = c.cname and e.city = d.city))) Note: This is a difficult query. The key is the double negation (using not exists). This one really works in SQLServer.
June 2, 2015R&G: Database Management Systems 7 An alternative solution: Find all the companies located in every city in which the FutureShop is located. (SQL) select c.cname from company c where not exists (select c2.city from company c2 except (select C3.city from company C3 where c3.name='future shop' and c3.city=c.city) Note: This is a solution in most students’ homework which follows the textbook. However, SQLServer does not have except and we cannot judge whether it really works (although we still give the points)
June 2, 2015R&G: Database Management Systems 8 Find the number of managers who manage more than 5 employees living in Richmond select count(manager) from person P, manages M where P.pname = M.pname and city = “Richmond” group by manager having count (pname) > 5
June 2, 2015R&G: Database Management Systems 9 Thank you !!!