• No results found

CSE132A Solutions HW 1

N/A
N/A
Protected

Academic year: 2021

Share "CSE132A Solutions HW 1"

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

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 thenames of suppliers who supply some red part.

πsname(πsid((πpidσcolor=′red′P arts)⊲⊳ Catalog)⊲⊳ suppliers)

{T :< sname > | ∃T1∈Suppliers(∃X∈P arts(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(P arts(P, Q, R)∧R=′red∧∃K Catalog(X, P, K))}

SELECT S.sname

FROM Suppliers S, Parts P, Catalog C

WHERE P.color=’red’ AND C.pid=P.pid AND C.sid=S.sid

2. (3 pts): Find thesids of suppliers who supply some red part or are at 221 Packer Street.

πsid((πpidσcolor=′red′P arts)⊲⊳ Catalog)∪πsidσaddress=′221P acker Str′Suppliers

{T :< sid > | ∃T1∈Catalog(∃X∈P arts(X[color] =′redX[pid] =T1[pid])T[sid] =T1.sid]) ∨∃T2∈Suppliers(T2[address] =′221P acker StrT[sid] =T2[sid])}

{(X:sid)| ∃Y∃Z Catalog(X, Y, Z)∧ ∃A∃B (P arts(Y, A, B)∧B=′red′)

(2)

FROM Suppliers S

WHERE S.address = ’221 Packer Str’) UNION

(SELECT C.sid

FROM Parts P, Catalog C

WHERE P.color = ’red’ AND P.pid = C.pid)

3. (3 pts): Find thesids of suppliers who supply some red part and some green part.

πsid((πpidσcolor=′red′P arts)⊲⊳ Catalog)∩πsid((πpidσcolor=′green′P arts)⊲⊳ Catalog)

{T :< sid > | ∃T1∈Catalog(∃X∈P arts(X[color] =′redX[pid] =T1[pid])

∧∃T2∈Catalog(∃Y ∈P arts(Y[color] =′greenY[pid] =T2[pid])T1[sid] =T2[sid]T[sid] =T1[sid])

{S:sid| ∃P1∃C1∃N1∃L1(Catalog(S, P1, C1)∧P arts(P1, N1, L1)∧L1 =′red) ∧∃P2∃C2∃N2∃L2(Catalog(S, P2, C2)∧P arts(P2, N2, L2)∧L2 =′green)}

SELECT C1.sid

FROM Catalog C1, Parts P1, Catalog C2, Parts P2 WHERE 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 thesids of suppliers who supply every part.

(πsid,pid(Suppliers ⊲⊳ Catalog))÷(πpidP arts)

{T :< sid > | ∃S∈Suppliers∀P ∈P arts∃C∈Catalog(C[sid] =S[sid]∧P[pid] =C[pid]∧T[sid] =S[sid])} •

{(X:sid)| ∃SN∃A Suppliers(X, SN, A)∧∀P∀P N∀L(P arts(P, P N, L)→ ∃C Catalog(X, P, C))}

SELECT S.sid FROM Suppliers S

WHERE (SELECT C.pid FROM Catalog C WHERE C.sid = S.sid) CONTAINS (SELECT pid FROM Parts)

5. (6 pts): Find thesids of suppliers who supply every red or green part.

(πsid,pid(Suppliers ⊲⊳ Catalog))÷(πpidσcolor=′red′∨color=′green′P arts)

{T:< sid > | ∃S∈Suppliers∀P ∈P arts(P[color] =′redP[color] =green) → ∃C∈Catalog(C[sid] =S[sid]∧P[pid] =C[pid]∧T[sid] =S[sid])}

(3)

{(X:sid)| ∃SN∃A Suppliers(X, SN, A)∧∀P∀P N∀L((P arts(P, P N, L)∧(L=′red′∨L=′green′))

→ ∃C Catalog(X, P, C))}

SELECT S.sid FROM Suppliers S

WHERE (SELECT C.pid FROM Catalog C WHERE C.sid = S.sid)

CONTAINS (SELECT pid FROM Parts WHERE color = ’red’ OR color = ’green’) 6. (6 pts): Find thesids of suppliers who supply every red part or supply every green part.

(πsid,pid(Suppliers ⊲⊳ Catalog))÷(πpidσcolor=′red′P arts)

(πsid,pid(Suppliers ⊲⊳ Catalog))÷(πpidσcolor=′green′P arts)

{T :< sid > |

∃S∈Supp∀P ∈P arts(P[color] =′red→ ∃CCat(C[sid] =S[sid]P[pid] =C[pid]T[sid] =S[sid])

∃S ∈Supp∀P ∈P arts(P[color] =′ green→ ∃CCat(C[sid] =S[sid]P[pid] =C[pid]T[sid] =S[sid])}

{(X:sid)| ∃SN∃A Suppliers(X, SN, A)∧∀P∀P N∀L((P arts(P, P N, L)∧L=′red′)→ ∃C Catalog(X, P, C))

∃SN∃A Suppliers(X, SN, A)∧∀P∀P N∀L((P arts(P, P N, L)∧L=′green′)→ ∃C Catalog(X, P, C))}

(SELECT S.sid FROM Suppliers S

WHERE (SELECT C.pid FROM Catalog C WHERE C.sid = S.sid) CONTAINS (SELECT pid FROM Parts WHERE color = ’red’)) UNION

(SELECT S.sid FROM Suppliers S

WHERE (SELECT C.pid FROM Catalog C WHERE C.sid = S.sid)

CONTAINS (SELECT pid FROM Parts WHERE color = ’green’))

7. (9 pts): Find pairs ofsids such that the supplier with the firstsidcharges more for some part than the supplier with the secondsid.

πsid,sid′σcost>cost′(Catalog ⊲⊳ δsid,cost7→sid′,cost′Catalog)

{T :< sid, sid′> | ∃CCatalogCCatalog

C[pid] =C′[pid]∧C[cost]> C′[cost]∧T[sid] =C[sid]∧T[sid′] =C[sid]}

(4)

• FROM Catalog C, Catalog C’

WHERE C.pid = C’.pid AND C.cost>C’.cost

8. (6 pts): Find thepids of parts supplied by at least two different suppliers.

πpidσsid6=sid′(Catalog ⊲⊳ δsid,cost7→sid′,cost′Catalog)

{T :< pid > | ∃C∈Catalog∃C′ ∈Catalog C[pid] =C′[pid]∧C[sid]6=C′[sid]∧T[pid] =C[pid]} •

{(P)| ∃S∃S′∃C∃C′ Catalog(S, P, C)∧Catalog(S′, P, C′)∧S 6=S′}

SELECT DISTINCT C.pid FROM Catalog C, Catalog C’

WHERE C.pid = C’.pid AND C.sid<>C’.sid

9. (9 pts): Find thepids of the most expensive parts supplied by suppliers named Yosemite Sham.

temp←πpid,costσsname=′Y osemite Sham′(Catalog ⊲⊳ Suppliers)

πpidtemp\πpidσcost<cost′(temp×δpid,cost7→pid′,cost′temp)

| {z }

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 SELECT C.pid, C.cost

FROM Catalog C, Suppliers S

WHERE C.sid=S.sid AND S.sname = ’Yosemite Sham’ SELECT T.pid

FROM TEMP T WHERE T.pid

NOT IN (SELECT T1.pid FROM TEMP T1, TEMP T2 WHERE T1.cost<T2.cost)

10. (9 pts): Find thepids 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).

(5)

T ooExpensive←πpidσcost>$200Catalog

SuppliedByAll←πpid,sidCatalog÷πsidSuppliers

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

SELECT pid FROM Catalog WHERE cost>$200

CREATE VIEW SuppliedByAll AS SELECT C.pid

FROM Catalog C

WHERE (SELECT C1.sid FROM Catalog C1 WHERE C1.pid = C.pid) CONTAINS (SELECT sid FROM Supplier)

SELECT pid

FROM SuppliedByAll

References

Related documents