0% found this document useful (0 votes)
319 views5 pages

CSE132A Solutions HW 1: Sname Sid Pid Color Red

The document contains a schema definition for Suppliers, Parts, and Catalog relations and 10 queries written in relational algebra, tuple relational calculus, domain relational calculus, and SQL to retrieve information from the relations. The queries find suppliers of red parts, suppliers by location or part type, pairs of suppliers with different part prices, and parts supplied by all or certain suppliers within price limits.

Uploaded by

Maya Jenny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
319 views5 pages

CSE132A Solutions HW 1: Sname Sid Pid Color Red

The document contains a schema definition for Suppliers, Parts, and Catalog relations and 10 queries written in relational algebra, tuple relational calculus, domain relational calculus, and SQL to retrieve information from the relations. The queries find suppliers of red parts, suppliers by location or part type, pairs of suppliers with different part prices, and parts supplied by all or certain suppliers within price limits.

Uploaded by

Maya Jenny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 P arts) Catalog) suppliers)

{T :< sname > | T 1 Suppliers(X P arts(X[color] = red


Y Catalog(Y [pid] = X[pid] Y [sid] = T 1[sid])) T [sname] = T 1[sname]}

{Y : sname | XZ Suppliers(X, Y, Z)P QR(P arts(P, Q, R)R = red K Catalog(X, P, K))}

SELECT
FROM
WHERE

S.sname
Suppliers S, Parts P, Catalog C
P.color=red AND C.pid=P.pid AND C.sid=S.sid

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

sid ((pid color= red P arts) Catalog) sid address= 221

P acker Str Suppliers

{T :< sid > | T 1 Catalog(X P arts(X[color] = red X[pid] = T 1[pid])T [sid] = T 1.sid])
T 2 Suppliers(T 2[address] = 221 P acker Str T [sid] = T 2[sid])}

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


P Q Suppliers(X, P, Q) Q = 221 P acker Str }
1

(SELECT
FROM
WHERE
UNION
(SELECT
FROM
WHERE

S.sid
Suppliers S
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 P arts) Catalog) sid ((pid color= green P arts) Catalog)

{T :< sid > | T 1 Catalog(X P arts(X[color] = red X[pid] = T 1[pid])


T 2 Catalog(Y P arts(Y [color] = green Y [pid] = T 2[pid])T 1[sid] = T 2[sid]T [sid] = T 1[sid])

{S : sid | P 1C1N 1L1(Catalog(S, P 1, C1) P arts(P 1, N 1, L1) L1 = red )


P 2C2N 2L2(Catalog(S, P 2, C2) P arts(P 2, N 2, L2) L2 = green )}

SELECT
FROM
WHERE

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 P arts)

{T :< sid > | S SuppliersP P artsC 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
FROM
WHERE

S.sid
Suppliers S
(SELECT C.pid FROM Catalog C WHERE C.sid = S.sid)
CONTAINS (SELECT pid FROM 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 P arts)

{T :< sid > | S SuppliersP P arts (P [color] = red P [color] = green )


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)(L = red L = green ))
C Catalog(X, P, C))}

SELECT
FROM
WHERE

S.sid
Suppliers S
(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 the sids 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 SuppP P arts(P [color] = red C Cat (C[sid] = S[sid]P [pid] = C[pid]T [sid] = S[sid])

S SuppP P arts(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 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
FROM
WHERE

UNION
(SELECT
FROM
WHERE

S.sid
Suppliers S
(SELECT C.pid FROM Catalog C WHERE C.sid = S.sid)
CONTAINS (SELECT pid FROM Parts WHERE color = red))
S.sid
Suppliers S
(SELECT C.pid FROM Catalog C WHERE C.sid = S.sid)
CONTAINS (SELECT pid FROM Parts WHERE 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,cost7sid ,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 CC Catalog(S, P, C) Catalog(S , P, C ) C > C }


3

SELECT
FROM
WHERE

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 sid6=sid (Catalog sid,cost7sid ,cost Catalog)

{T :< pid > | C Catalog C Catalog


C[pid] = C [pid] C[sid] 6= C [sid] T [pid] = C[pid]}

{(P ) | SS CC Catalog(S, P, C) Catalog(S , P, C ) S 6= S }

SELECT
FROM
WHERE

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,cost7pid ,cost temp)


{z
}
|
not most expensive

{T :< pid > | T 1 Catalog(X Suppliers


(X.sname = Y osemite Sham X.sid = T 1.sid)
(Z Catalog(Z.sid = T 1.sid Z.cost > T 1.cost)))
T [pid] = T 1[pid]}

{(P : pid) | SC Catalog(S, P, C) SN 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 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).


T ooExpensive 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) | SC 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
WHERE pid NOT IN TooExpensive

You might also like