Sie sind auf Seite 1von 4

CSE 462 Classwork #1: Relational Algebra

Name: Date: July 5, 2011

***** You have 50 minutes to complete the work in class. Due date is July 7, before lecture starts. *****

Directions. Read and understand each problem before you start to answer. Provide complete answers, show your work, and do not forget to explain and/or justify an answer when requested. Strive for brevity, clarity, and completeness in your answers. Answer on the spaces provided. May you need extra space, you may attach additional sheets. Use only relational algebra constructs covered in class. You are encouraged to use linear notation to simplify your answers. Set operators: union (), intersection (), dierence (). Selection: (R). Projection: L (R). Cartesian product: R S . Natural join: R Theta-join: R

S. S.

Quotient: R S . Renaming: S (B1 ,...,Bk ) (R) or S (R). For renaming, you may also use any other syntax discussed in class. Consider the schema below for an online store. Refer to this schema in order to answer the questions in the test. Customer(cId, name, city, phone) Product(sku, productName, unitPrice) Orders(oId, cId, date) OrderEntry(oId, sku, qty) Observations: Keys are underlined. Values of cId in Orders appear as values of cId in Customer. Values of oId in OrderEntry appear as values of oId in Orders. Values of sku in OrderEntry appear as values of sku in Product.

July 5, 2011

CSE 462, Relational Algebra

a) Find the customer(s) who live in the city of Bualo and placed an order having one entry with a quantity of at least 30 and one with a quantity of at most 20. List their respective cId and name. (Note: the order may have additional entries, but we do not care about those.) Bualo Answer cId,name (city=Bualo (Customer)) cId (q130q220 (Orders Bualo OrdersReq OrderEntry(oId, p1, q1) OrderEntry(oId, p2, q2)))

OrdersReq

b) List the sku of the most expensive product(s) in the store. Cheap Answer sku (Product
unitPrice<up2

Product(sku2, name2, up2))

sku (Product) Cheap

c) Find the product(s) that have been ordered exactly twice. List their respective sku, productName, and unitPrice. TwoPlus ThreePlus Answer sku,oId1,oId2 (OrderEntry(oId1, sku, q1) Product
oId1<oId2sku=sku2

OrderEntry(oId2, sku2, q2)) OrderEntry(oId3, sku3, q3))

sku,oId1,oId2,oId3 (TwoPlus(sku, oId1, oId2) (sku (TwoPlus) sku (ThreePlus))

oId2<oId3sku=sku3

d) Find the customer(s) who never placed any order. List their respective cId, and name. cId,name (Customer) cId,name (Customer Order)

e) Find the product(s) that have only been ordered by customers who live in Bualo. List their respective sku, productName, and unitPrice. NonBualo Answer sku (city
Bualo (Customer

Order

OrderEntry))

Product

(sku (Product) NonBualo)

f) Find the product(s) that only appear in orders having a single entry. List their respective sku and price. (Note: this is dierent from listing the products that appear in orders having a single entry!) NonSingle Answer sku (OrderEntry sku,unitPrice (Product
oId=oId2sku sku2

OrderEntry(oId2, sku2, q2))

(sku (OrderEntry) NonSingle))

July 5, 2011 Consider the relational algebra expressions below. Q1) oId (Orders) Q2) oId (Orders Q3) oId (Orders Orders(oId2, cId2, d2)) oId2date=d2 Orders(oId2, cId2, d2))

CSE 462, Relational Algebra

cId cId2date=d2 oId

a) Briey describe what each expression computes. Q1 : set of identiers of all orders Q2 : set of identiers of orders from days in which at least two distinct customers placed orders Q3 : set of identiers of orders from days in which at least two distinct orders were placed

b) Say whether each of the containment relationships below holds. For each one that does not hold, provide an instance of Orders that justies your answer. Q1 Q2? ( ) Yes ( ) No No. Any single tuple in orders is a counterexample. Q1 Q3? ( ) Yes ( ) No No. Any single tuple in orders is a counterexample. Q2 Q3? ( ) Yes ( ) No Yes.

Q1 Q2? ( ) Yes ( ) No Yes.

Q1 Q3? ( ) Yes ( ) No Yes.

Q2 Q3? ( ) Yes ( ) No No. {(1,1,1/1/11),(2,1,1/1/11)} is a counterexample. Q2 returns an empty set for this instance, Q3 does not.

July 5, 2011 ADDITIONAL PAGE

CSE 462, Relational Algebra

Das könnte Ihnen auch gefallen