CSE132A Solutions HW 1 Problem 2. [60pts] Consider the following schema: Suppliers(sid:integer, sname: string, address: string) Parts(pid:integer, pname: string, color: string) Catalog(sid:integer, pid: integer, cost: real) The key fields are underlined, and the domain of each field is listed after the field name. The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in (a) relational algebra, (b) tuple relational calculus, (c) domain relational calculus and (d) SQL. 1. (3 pts): Find the names of suppliers who supply some red part. π sname (π sid ((π pid σ color= red Parts) Catalog) suppliers) {T :< sname > T1 Suppliers( X Parts(X[color] = red Y Catalog(Y [pid] = X[pid] Y [sid] = T1[sid])) T[sname] = T1[sname]} {Y : sname X Z Suppliers(X, Y, Z) P Q R(Parts(P, Q, R) R = red K Catalog(X, P, K))} S.sname, Parts P, Catalog C P.color= red AND C.pid=P.pid AND C.sid= 2. (3 pts): Find the sids of suppliers who supply some red part or are at 221 Packer Street. π sid ((π pid σ color= red Parts) Catalog) π sidσ address= 221 Packer Str Suppliers {T :< sid > T1 Catalog( X Parts(X[color] = red X[pid] = T1[pid]) T[sid] = T1.sid]) T2 Suppliers(T2[address] = 221 Packer Str T[sid] = T2[sid])} {(X : sid) Y Z Catalog(X, Y, Z) A B (Parts(Y, A, B) B = red ) P Q Suppliers(X, P, Q) Q = 221 Packer Str } 1
( UNION ( S.address = 221 Packer Str ) C.sid Parts P, Catalog C P.color = red AND P.pid = C.pid) 3. (3 pts): Find the sids of suppliers who supply some red part and some green part. π sid ((π pid σ color= red Parts) Catalog) π sid((π pid σ color= green Parts) Catalog) {T :< sid > T1 Catalog( X Parts(X[color] = red X[pid] = T1[pid]) T2 Catalog( Y Parts(Y [color] = green Y [pid] = T2[pid]) T1[sid] = T2[sid] T[sid] = T1[sid]) {S : sid P1 C1 N1 L1(Catalog(S, P1, C1) Parts(P1, N1, L1) L1 = red ) P2 C2 N2 L2(Catalog(S, P2, C2) Parts(P2, N2, L2) L2 = green )} C1.sid Catalog C1, Parts P1, Catalog C2, Parts P2 C1.pid=P1.pid AND P1.color= red AND C2.pid=P2.pid AND P2.color= green AND C1.sid=C2.sid 4. (6 pts): Find the sids of suppliers who supply every part. (π sid,pid (Suppliers Catalog)) (π pid Parts) {T :< sid > S Suppliers P Parts C Catalog (C[sid] = S[sid] P[pid] = C[pid] T[sid] = S[sid])} {(X : sid) SN A Suppliers(X, SN, A) P PN L (Parts(P, PN, L) C Catalog(X, P, C))} ( C.pid Catalog C C.sid = ) CONTAINS ( pid Parts) 5. (6 pts): Find the sids of suppliers who supply every red or green part. (π sid,pid (Suppliers Catalog)) (π pid σ color= red color= green Parts) {T :< sid > S Suppliers P Parts (P[color] = red P[color] = green ) C Catalog (C[sid] = S[sid] P[pid] = C[pid] T[sid] = S[sid])} 2
{(X : sid) SN A Suppliers(X, SN, A) P PN L ((Parts(P, PN, L) (L = red L = green )) C Catalog(X, P, C))} ( C.pid Catalog C C.sid = ) CONTAINS ( pid Parts color = red OR color = green ) 6. (6 pts): Find the sids of suppliers who supply every red part or supply every green part. (π sid,pid (Suppliers Catalog)) (π pid σ color= red Parts) (π sid,pid (Suppliers Catalog)) (π pid σ color= green Parts) {T :< sid > S Supp P Parts(P[color] = red C Cat (C[sid] = S[sid] P[pid] = C[pid] T[sid] = S[sid]) S Supp P Parts(P[color] = green C Cat (C[sid] = S[sid] P[pid] = C[pid] T[sid] = S[sid])} {(X : sid) SN A Suppliers(X, SN, A) P PN L ((Parts(P, PN, L) L = red ) C Catalog(X, P, C)) SN A Suppliers(X, SN, A) P PN L ((Parts(P, PN, L) L = green ) C Catalog(X, P, C))} ( UNION ( ( C.pid Catalog C C.sid = ) CONTAINS ( pid Parts color = red )) ( C.pid Catalog C C.sid = ) CONTAINS ( pid Parts color = green )) 7. (9 pts): Find pairs of sids such that the supplier with the first sid charges more for some part than the supplier with the second sid. π sid,sid σ cost>cost (Catalog δ sid,cost sid,cost Catalog) {T :< sid, sid > C Catalog C Catalog C[pid] = C [pid] C[cost] > C [cost] T[sid] = C[sid] T[sid ] = C [sid]} {(S, S ) P C C Catalog(S, P, C) Catalog(S, P, C ) C > C } 3
C.sid, C.sid AS sid Catalog C, Catalog C C.pid = C.pid AND C.cost > C.cost 8. (6 pts): Find the pids of parts supplied by at least two different suppliers. π pid σ sid sid (Catalog δ sid,cost sid,cost Catalog) {T :< pid > C Catalog C Catalog C[pid] = C [pid] C[sid] C [sid] T[pid] = C[pid]} {(P) S S C C Catalog(S, P, C) Catalog(S, P, C ) S S } DISTINCT C.pid Catalog C, Catalog C C.pid = C.pid AND C.sid <> C.sid 9. (9 pts): Find the pids of the most expensive parts supplied by suppliers named Yosemite Sham. temp π pid,cost σ sname= Y osemite Sham (Catalog Suppliers) π pid temp \ π pid σ cost<cost (temp δ pid,cost pid,cost temp) } {{ } not most expensive {T :< pid > T1 Catalog( X Suppliers (X.sname = Y osemite Sham X.sid = T1.sid) ( Z Catalog(Z.sid = T1.sid Z.cost > T1.cost))) T[pid] = T1[pid]} {(P : pid) S C Catalog(S, P, C) S N ASuppliers(S, N, A) N = Y osemite Sham ( P C Catalog(S, P, C ) C > C)} CREATE VIEW TEMP(pid,cost) AS C.pid, C.cost Catalog C, C.sid= AND S.sname = Yosemite Sham T.pid TEMP T T.pid NOT IN ( T1.pid TEMP T1, TEMP T2 T1.cost < T2.cost) 10. (9 pts): Find the pids of parts supplied by every supplier at less than $200. (If any supplier either does not supply the part or charges more than $200 for it, the part is not selected). 4
TooExpensive π pid σ cost>$200 Catalog SuppliedByAll π pid,sid Catalog π sid Suppliers SuppliedByAll \ T ooexpensive {T :< pid > C Catalog S Supplier C Catalog C [sid] = S[sid] C [pid] = C[pid] ( C Catalog C [pid] = C[pid] C [cost] > $200)} {(P : pid) S C Catalog(S, P, C) S N A Supplier(S, N, A) C Catalog(S, P, C ) ( S C Catalog(S, P, C ) C > $200)} CREATE VIEW TooExpensive AS pid Catalog cost > $200 CREATE VIEW SuppliedByAll AS C.pid Catalog C ( C1.sid Catalog C1 C1.pid = C.pid) CONTAINS ( sid Supplier) pid SuppliedByAll pid NOT IN TooExpensive 5