Løsningsforslag for oppgave 5.2.1 Harald Askestad 28. februar 2002

advertisement
Løsningsforslag for oppgave 5.2.1
Harald Askestad
28. februar 2002
a) What PC models have a speed of at least 1000?
πmodel (σspeed>=1000 (PC))
b) Which manufacturers make laptops with a hard disk of at least one GB?
L1 := πmodel (σhd>=1 (Laptop))
Answer := πmaker (L1 1 Product)
c) Find the model number and price of all products (of any type) made by
manufacturer B.
BModel := πmodel (σmaker=0 B0 (Product))
BPC := πmodel,price (BModel 1 PC)
BPrinter := πmodel,price (BModel 1 Printer)
BLaptop := πmodel,price (BModel 1 Laptop)
Answer := BPC ∪ BPrinter ∪ BLaptop
d) Find the model numbers of all color laser printers.
πmodel (σcolor AND type=0 laser0 (Printer))
e) Find those manufacturers that sell laptops, but not PC’s.
LaptopMaker := πmaker (σtype=0 laptop0 (Product))
PCMaker := πmaker (σtype=0 pc0 (Product))
Answer := LaptopMaker − PCMaker
1
f) Find those hard-disk sizes that occur in two or more PC’s.
PC1 := PC
PC2 := PC
1
Answer := πPC1.hd (PC1
PC1.hd=PC2.hd AND
PC2)
PC1.model<>PC2.model
g) Find those pairs of PC models that have both the same speed and RAM.
A pair shoud be listed only once.
PC1 := PC
PC2 := PC
1
Answer(M1,M2) := πPC1.model,PC2.model (PC1
PC1.speed=PC2.speed AND
PC1.ram=PC2.ram AND
PC1.model<PC2.model
h) Find those manufacturers of at least two different computers (PC’s or
laptops) with speeds of at least 700.
Computers := πmodel (σspeed>=700 (PC)) ∪
πmodel (σspeed>=700 (Laptop))
M1(maker1,model1) := πmaker,model (Computers 1 Product)
M2(maker2,model2) := M1
Answer := πmaker1 (M1
1
maker1=maker2 AND
M2)
model1<>model2
i) Find the manufacturer(s) of the computer (PC or laptop) with the highest available speed.
C1 := πmodel,speed (PC) ∪ πmodel,speed (Laptop)
C2 := C1
Slowest := πC1.model (C1
1
C1.speed<C2.speed
Quickest := πmodel (C1) − Slowest
Answer := πmaker (Quickest 1 Product)
2
C2)
PC2)
j) Find the manufacturers of PC’s with at least three different speeds.
P1(maker,s1) := πmaker,speed (PC 1 Product)
P2(maker,s2) := P1
P3(maker,s3) := P1
P4(maker,s1,s2,s3) := P1 1 P2 1 P3
Answer := πmaker (σs1<>s2 AND s1<>s3 AND s2<>s3 (P4))
k) Find the manufacturers who sell exactly three different models of PC.
P1(maker,m1) := πmaker,model (σtype=0 pc0 (Product))
P2(maker,m2) := P1
P3(maker,m3) := P1
P4(maker,m4) := P1
P5(maker,m1,m2,m3) := P1 1 P2 1 P3
3different := πmaker (σm1<>m2 AND m1<>m3 AND m2<>m3(P5))
P6(maker,m1,m2,m3,m4) := P5 1 P4
4different := πmaker (σ
m1<>m2 AND m1<>m3 AND m1<>m4 AND
m2<>m3 AND m2<>m4 AND
m3<>m4
Answer := 3different − 4different
Merk at løsningen på oppgave j og k kan gjøres mer effektiv i praksis ved å
flytte seleksjonsbetingelsen opp i join-uttrykkene. Slik den er nå, vil naturlig join produsere en mengde uinteressante tupler som ikke blir silt ut før
i seleksjonen. Jeg valgte likevel den ineffektive løsningen fordi jeg tror den
er lettere å lese.
3
(P6))
Download