Database Systems Complete Book Solutions
Database Systems Complete Book Solutions
a)
Attributes of relation Accounts are acctNo , type and balance.
Attributes of relation Customers are firstName , lastName, idNo and account.
b)
For the relation Accounts:
c)
d)
1
e)
f)
g)
Just swap the attributes (whatever you like).
Exercise 2.2
Exercise 2.3
a)
1 CREATE TABLE Product(
2 maker VARCHAR(50),
3 model INT,
4 type VARCHAR(25),
5 PRIMARY_KEY(maker)
6 );
b)
2
1 CREATE TABLE PC(
2 model INT,
3 speed FLOAT,
4 ram INT,
5 hd INT,
6 price FLOAT,
7 PRIMARY_KEY(model)
8 );
c)
1 CREATE TABLE Laptop(
2 model INT,
3 speed FLOAT,
4 ram INT,
5 hd INT,
6 screen FLOAT,
7 price FLOAT,
8 PRIMARY_KEY(model)
9 );
d)
1 CREATE TABLE Printer(
2 model INT,
3 color BOOLEAN,
4 type VARCHAR(25),
5 price FLOAT,
6 PRIMARY_KEY(model)
7 );
e)
1 ALTER TABLE Printer DROP color
f)
1 ALTER TABLE Laptop ADD od VARCHAR(25) DEFAULT 'none'
Exercise 3.2
a)
1 CREATE TABLE classes(
2 class VARCHAR(50) UNIQUE,
3 type VARCHAR(50),
4 country VARCHAR(50),
5 numGums INT,
6 bore FLOAT,
7 displacement INT
8 );
3
b)
1 CREATE TABLE Ships(
2 name VARCHAR(50),
3 class VARCHAR(59),
4 launched INT
5 );
c)
1 CREATE TABLE Battles(
2 name VARCHAR(50) UNIQUE,
3 data DATE
4 );
d)
1 CREATE TABLE Outcomes(
2 ship VARCHAR(50),
3 battle VARCHAR(50),
4 result VARCHAR(50)
5 );
e)
1 ALTER TABLE Classes DROP bore;
f)
1 ALTER TABLE Ships ADD yard VARCHAR(50);
a)
R1 := σspeed≥3.00 (P C)
R2 := πmodel (R1)
b)
R1 := σhd≥100 (Laptop)
R2 := P roduct ▷◁ (R2)
R3 := πmaker (R3)
c)
4
R1 := σmaker=B (P roduct ▷◁ P C)
R2 := σmaker=B (P roduct ▷◁ Laptop)
R3 := σmaker=B (P roduct ▷◁ P rinter)
R4 := πmodel,price (R1) ∪ πmodel,price (R2) ∪ πmodel,price (R3)
d)
e)
R1 := σtype=laptop (P roduct)
R2 := σtype=pc (P roduct)
R3 := πmaker (R1) − πmaker (R2)
f)
R1 := ρP C1 (P C)
R2 := ρP C2 (P C)
R3 := R1 ▷◁P C1.hd=P C2.hd AND P C1.model<>P C2.model R2
R4 := πhd (R3)
g)
The most important thing here is to notice that a pair should be listed only once. Here we use
PC1.model < PC2.model to achieve this functionality.
R1 := ρP C1 (P C)
R2 := ρP C2 (P C)
R3 := R1 ▷◁P C1.speed=P C2.speed AND P C1.ram=P 2.ram AND P C1.model<P C2.model R2
h)
5
i)
The most tricky thing is to understand how to find the way to get the maximum. In C, we could
use a temp variable to record the current maximum in the loop. However, we cannot do that
using relation algebra.
Here, I give an example, suppose we have an only one attribute relation A. For simplicity, I
choose A = (a1 , · · · , an )T . I do the cartesian product. If there is a maximum n, we could get
that for all ai , i = 1, . . . , n, n ≥ ai . Then idea comes.
R1 := πmodel,speed (P C)
R2 := πmodel,speed (Laptop)
R3 := R1 ∪ R2
R4 := ρR4(model2,speed2) (R3)
R5 := πmodel,speed (R3 ▷◁speed<speed2 R4)
R6 := R3 − R5
R7 := πmaker (R6 ▷◁ P roduct)
j)
R1 := πmaker,speed (P C ▷◁ P roduct)
R2 := ρR2(maker2,speed2) (R1)
R3 := ρR3(maker3,speed3) (R1)
R4 := R1 ▷◁maker=maker2 AND speed<>speed2 R2
R5 := R4 ▷◁maker=maker3 AND speed3<>speed2 AND speed3<>speed R3
R6 := πmaker (R5)
k)
R1 := πmaker,model (P C ▷◁ P roduct)
R2 := ρR2(maker2,model2) (R1)
R3 := ρR3(maker3,model3) (R1)
R4 := ρR4(maker4,model4) (R1)
R5 := R1 ▷◁maker=maker2 AND model<>model2 R2
R6 := R3 ▷◁maker=maker3 AND model3<>model2 AND model3<>model R5
R7 := R4 ▷◁maker4=maker AND (model4=model OR model4=model2 OR model4=model3) R6
R8 := πmaker (R7)
Exercise 4.2
It’s a dirty job. I omit detail here. But do remember a query tree is a tree data structure
representing a relational algebra expression.
6
Exercise 4.3
a)
R1 := σbore≥16 (Classes)
R2 := πclass,country (R1)
b)
R1 := σlaunched<1921 (Ships)
R2 := πname (R1)
c)
d)
e)
R1 := σbattle=Guadalcanal (Outcomes)
R2 := Ships ▷◁ship=name R1
R3 := Classes ▷◁ R2
R4 := πname,displacement,numGuns (R3)
f)
R1 := πship (Outcomes)
R2 := ρR2(name) (R1)
R3 := πname (Ships ∪ R2)
g)
R1 := πclass (Classes)
R2 := πclass (σclass<>name (Ships))
R3 := R1 − R2
7
h)
i)
Exercise 4.4
Exercise 4.5
Natural join does not use any comparison operator. It does not concatenate the way a Cartesian
product does. We can perform a Natural Join only if there is at least one common attribute that
exists between two relations. In addition, the attributes must have the same name and domain.
Exercise 4.6
• A(a1 , a2 , . . . , an )
• B(b1 , b2 , . . . , bn )
First, the ∩ operation is monotone. Assume that C = A ∩ B. When A adds a tuple an+1 , C
may be not changed or become C ′ = C ∪ an+1 such that C ⊆ C ′ . For other situations, the idea
is the same.
Second, the σ operator is monotone. Assume that C = σc (A). When A adds a tuple an+1 , C
may be not changed or become C ′ = C ∪ an+1 such that C ⊆ C ′ .
Exercise 4.7
a)
• max = n + m
• min = max(n, m)
8
b)
• max = n × m
• min = 0
c)
• max = n × m
• max = 0
d)
• max = n
• min = 0
Exercise 4.8
• π1...n (R ▷◁ S)
• π1...n (σR.1=S.1...R.K=S.K (R × S))
• R ▷◁1,...k (S)
Exercise 4.9
R − π1,...,n (R ▷◁ S)
Exercise 4.10
An intuitive property of the division operator of the relational algebra is simply that it is the
inverse of the cartesian product.
a)
b)
9
c)
d)
R1 := πmaker,model,speed (P roduct ▷◁ P C)
R2 := πmaker,speed (P roduct ▷◁ Laptop)
R3 := πmodel (R1 ▷◁R1.maker=R2.maker AND R1.speed>R2.speed R2)
R4 := πmodel (R3) = ∅
e)
R1 := πram,price (P C)
R2 := πram,price (Laptop)
Exercise 5.2
a)
b)
c)
d)
e)
10
R1 := σnumGuns>9 (Classes)
R2 := σnumGuns<9 (Classes)
R3 := (R1 ▷◁ Ships) ▷◁ Battles
R4 := (R2 ▷◁ Ships) ▷◁ σresult=sunk (Battles)
R5 := πbattlename (R3) ▷◁ πbattlename (R4) = ∅
Exercise 5.3
Exercise 5.4
If we have 3 relational-algebra expressions for example E1, E2 and E3 we can not express them
like E1 = E2 = E3. Thus, the answer is no.
The FD’s:
Possible keys are any attributes on the left hand side of the arrows.
Exercise 1.2
Let x, y, z are coordinates of the molecule and vx , vy , vz are the velocities of the corresponding
coordinates.
The FD’s:
• x, y, z → vx
• x, y, z → vy
• x, y, z → vz
11
x, y, z and vx , vy , vz are the keys because with these combination only we can get molecule in
the closed surface.
Exercise 1.3
a)
Apart A1 there are n − 1 attributes to be added. For each attributes, there are tow choices:
whether to include it or not. Due to the reason that set is unordered. Hence
|A1 | = 2n−1
b)
c)
d)
a)
First, we need to find all the closures of the subsets of the attributes.
AB, BC AND BD
c)
12
The superkeys are all those that contain one of those three keys. The proper superkeys are:
Exercise 2.2
i)
It is obvious that {A}+ = {A, B, C, D}. So there is no need to calculate the closures of subsets
which contain A. Use this idea, we can simplify the calculation.
ii)
iii)
13
{A_ _ _}+ = {A, B, C, D}
{B_ _ _}+ = {A, B, C, D}
{C_ _ _}+ = {A, B, C, D}
{D_ _ _}+ = {A, B, C, D}
A, B, C AND D
Subset{A, B, C, D} − keys
Exercise 2.3
a)
{A1 , A2 , · · · , An }+ = {A1 , A2 , · · · , An , B}
Thus proved.
b)
It can be easily proved by trivial FD.
c)
C 1 C 2 · · · C k → D ⇒ B 1 B 2 · · · B m E 1 E2 · · · Ej
A1 A2 · · · An → B 1 B 2 · · · B m ⇒ A1 A2 · · · An E 1 E 2 · · · E j → B 1 B 2 · · · B m E 1 E 2 · · · E j
A1 A2 · · · An E 1 E 2 · · · E j → D
d)
{A1 , A2 , · · · , An }+ = {A1 , A2 , · · · , An , B1 , B2 · · · Bm }
14
{C1 , C2 , · · · , Ck }+ = {C1 , C2 , · · · , Ck , D1 , D2 · · · Dj }
Exercise 2.4
a)
Attribute A represents Social Security Number and B represented a person’s name.
b)
Attribute A represents Social Security Number and B represented a person’s gender, C represents
a person’s name.
c)
Attribute A represents latitude and B represents longitude, C represents a point on the world
map.
Exercise 2.5
Consider the relationship R(A, B, C, D). Suppose there exists a non-trivial dependency A → B.
Then:
• There exist an attribute B that can be functionally determined by all other attributes.
• For some value of A the value of B can be functionally determined
It is given that no attribute can be functionally determined by all other attributes. This is
contradiction.
Exercise 2.6
X+ ⊆ Y +
Exercise 2.7
We prove it by concept, when we find the closure of X, it must go to the end. So for (X + )+ , it
can’t find any suitable attributes.
Exercise 2.8
a)
15
A→A
B→B
C→C
D→D
b)
ABC → D
c)
A→B
ABC → D
Exercise 2.9
• A → B, B → A, B → C, C → B
• A → B, B → C, C → A
• B → A, A → C, C → B
• C → A, A → B, B → C
• A → C, B → A, C → A, C → B
• C → A, A → B, B → A, B → C
• C → B, C → A, B → C, A → B
• B → C, B → A, A → B, C → B
• A → B, A → C, B → A, C → B
Exercise 2.10
a)
16
{A}+ = {A, D}, {B}+ = {B}, {C}+ = {C}
{AB}+ = {A, B, D, E}, {BC}+ = {B, C}
{AC}+ = {A, C, E}, {ABC}+ = {A, B, C, D, E}
From the above closure, A and C are the only attributes presents in S. So we can find FD in S:
A → B, C → B.
c)
For {AB}+ , A and B are the only attributes presents in S.For {AC}+ and {BC}+ , A, B and
C are the only attributes. So we can find FD in S: A → B, B → A, C → B, C → A.
d)
So we can find FD in S: A → B, B → C, C → A.
Exercise 2.11
A→B
B→C
B→D
A→B
B→C
Gives A → C. Thus, if FD F follows from the given FD’s, then F can be proved from the given
FD’s using Armstrong’s axioms.
17
3.3 Design of Relational Database Schemas
Exercise 3.1
a)
{AB}+ = {A, B, C, D}
{C}+ = {A, C, D}
{D}+ = {A, D}
So we can find that the FD’s in R1 are: C → D, D → A. D is not a superkey. So again, the FD
D → A is not in BCNF and we decompose R1 into R3 (A, D) and R4 (C, D).
b)
We simply compute {B}+ = {B, C, D}. Since A is not in the closure, there is a violation of the
BCNF.
By choosing FD B → C, we split the relation into R1 (B, C, D) and R2 (A, B).
Next, we need to find FD’s in R1 and R2 . There are only two attributes in R2 , so R2 is in BCNF.
{B}+ = {B, C, D}
{C}+ = {C}
{D}+ = {D}
{CD}+ = {C, D}
{AB}+ = {A, B, C, D}
{BC}+ = {A, B, C, D}
{CD}+ = {A, B, C, D}
{AD}+ = {A, B, C, D}
18
No BCNF violations, Done.
d)
{A}+ = {A, B, C, D}
{B}+ = {A, B, C, D}
{C}+ = {A, B, C, D}
{D}+ = {A, B, C, D}
{AB}+ = {A, B, C, D}
{DE}+ = {B, C, D, E}
{B}+ = {B, D}
19
Exercise 3.2
For A → B and A → C:
{A}+ = A, B, C
Exercise 3.3
Well, from Exercise 3.2, we can know that {A}+ will never be changed.So we can get the same
result.
Exercise 3.4
A B C D E
a b c d1 e1
a1 b c d e2
a b1 c d2 e
a)
The result of chasing test is below:
A B C D E
a b c d1 e1
a b c d e1
a b1 c d2 e
20
A B C D E
a b c d e
a b c d e2
a b1 c d2 e
The first row has no subscripted symbols and the decomposition is lossless
c)
The result of chasing test is below:
A B C D E
a b c d e
a1 b c d e2
a b1 c d1 e
The first row has no subscripted symbols and the decomposition is lossless
d)
A B C D E
a b c d1 e
a1 b c d e2
a b1 c d1 e
Exercise 4.2
a)
There is no relation that contain BE so this is not dependency preserving. B → E is not
preserved.
b)
There is relation {A, C, E} that contains ACE so AC → E is dependency preserving.
There is relation {B, C, D} that contains BCD so BC → D is dependency preserving.
c)
A → D and D → E are not preserved.
d)
None of the FD’s is preserved.
21
3.5 Third Normal Form
Exercise 5.1
a)
We first find all the keys.
{AB}+ = {A, B, C, D}
{BD}+ = {A, B, C, D}
Thus we can get the primes are {A, B, D}. For AB → C, AB is the key itself. And for C → D
and D → A, the right side consists of prime attributes. It is in 3NF.
b)
We first find all the keys.
{AB}+ = {A, B, C, D}
Thus we can get the primes are {A, B}. For B → C and B → D. B is not the superkey. And
neither the rights sides consist of prime attributes. Thus it violates 3NF.
Make relation R1 (A, B) and R2 (B, C, D) in which B is candidate key.
c)
We first find all the keys.
{AB}+ = {A, B, C, D}
{BC}+ = {A, B, C, D}
{CD}+ = {A, B, C, D}
{AD}+ = {A, B, C, D}
Thus we can get the primes are {A, B, C, D}. In all FDs, left side is a key. It is in 3NF.
d)
We first find all the keys.
{A}+ = {A, B, C, D}
{B}+ = {A, B, C, D}
{C}+ = {A, B, C, D}
{D}+ = {A, B, C, D}
22
e)
We first find all the keys.
{ABE}+ = {A, B, C, D, E}
Thus we can get the primes are {A, B, E}. For B → C It obviously violates 3NF
Make relation R1 (A, B, C, D), R2 (D, E, C) and R3 (A, B, E)
f)
For simplicity, we only give one answer here:
R1 (A, B, C, D) and R2 (D, E, B)
Exercise 5.2
a)
Key for the relation Courses is HS.
b)
FD’s are already minimal basis, as no attributes can be removed from the left side, right side is
having only single attributes and none of the FDcan be removed as given in the question.
c)
By using the 3NF synthesis algorithm, the final set of relations obtained will be CT , HRC,
HT R, HSR, CSG . All the relations are in BCNF.
Exercise 5.3
a)
Key for the relation Courses is IS.
b)
The given set of FD’s are the minimal basis.
c)
The final set of decomposed relations is SD, IB, ISQ and BO.
Exercise 5.4
A B C D E
a b c d1 e1
a b2 c2 d e2
a b c3 d3 e
23
A B C D E
a b c d1 e1
a b2 c2 d e2
a b c d3 e
A B C D E
a b c d e1
a b2 c2 d e2
a b c d e
Exercise 5.5
Need help
we can pair the B-value from any tuple with the value of the remaining attribute C from any
other tuple.
Thus the tuples (a, b1 , c2 ), (a, b1 , c3 ), (a, b2 , c1 ), (a, b2 , c3 ), (a, b3 , c1 ), and a, b3 , c2 are also in R.
Exercise 6.2
a)
From the description, we can get the following FD.
S → NB
CS → CN
CS → CB
AS → AM
S ↠ NB
S ↠ AS
S ↠ am
24
b)
• R1 (S, CS, A)
• R2 (S, N, B)
• R2 (CS, CN, B)
• R4 (AS, M )
Exercise 6.3
a)
Left side is not a key and holds more than one MVD so it violates 4NF.
We could decompose R into R1 (A, B), R2 (A, C) and R3 (A, D).
b)
For B ↠ CD, left side is not a key. So it violates 4NF.
We could decompose R into R1 (A, B), R2 (B, C, D).
c)
For B ↠ D, left side is not a key. It violates 4NF.
We could decompose R into R1 (A, B, C), R2 (B, D).
d)
We could easily calculate {A}+ = A, B, C, D, E. So all the left sides are keys. so it is in 4NF.
Exercise 6.4
• City is not determined by other four attributes as there can be same street address with
different city.
• Year is not determined as there can be different address of one star.
• Title is not determines as with two same movie, different address is present.
• Street is not determined as with two same title, street is different.
• Name is not determined as with two same year and same movie star have different address.
25
Exercise 7.1
Exercise 7.2
Exercise 7.3
Exercise 7.4
Exercise 1.1
Exercise 1.2
Exercise 1.3
Exercise 1.4
Exercise 1.5
Exercise 1.6
Exercise 1.7
Exercise 1.8
Exercise 1.9
Exercise 1.10
26
Exercise 2.1
Exercise 2.2
Exercise 2.3
Exercise 2.4
Exercise 2.5
Exercise 2.6
Exercise 2.7
Exercise 3.1
Exercise 3.2
Exercise 3.3
Exercise 4.1
Exercise 4.2
Exercise 4.3
Exercise 4.4
27
4.5 From E/R Diagrams to Relational Designs
Exercise 5.1
Exercise 5.2
Exercise 5.3
Exercise 5.4
Exercise 6.1
Exercise 6.2
Exercise 6.3
Exercise 6.4
28
Exercise 7.1
Exercise 7.2
Exercise 7.3
Exercise 7.4
Exercise 7.5
Exercise 7.6
Exercise 7.7
Exercise 7.8
Exercise 7.9
Exercise 7.10
Exercise 8.1
Exercise 8.2
Exercise 8.3
29
Exercise 9.1
Exercise 9.2
Exercise 9.3
Exercise 9.4
Exercise 9.5
Exercise 9.6
Exercise 9.7
Exercise 9.8
Exercise 10.1
Exercise 10.2
Exercise 10.3
Exercise 10.4
For simplicity, we assume that R has r tuples, S has t tuples and T has t tuples.
a)
For a bag:
Tuples((R ∪ S) ∪ T ) = (r + s) + t = r + s + t
Tuples(R ∪ (S ∪ T )) = r + (s + t) = r + s + t
For a set:
Each set can have a tuple only once. If each set has a common tuple then the result will have
the single occurrence.
b)
30
Tuples((R ∩ S) ∩ T ) = min(min(r, s), t)
Tuples(R ∩ (S ∩ T )) = min(r, min(s, t))
Bags are essentially sets that allow the appearance of a tuple more than once. In this case the
operations on a bag and set yield the same results.
c)
We let R = {a, b}, S = {b, c}, T = {c, d}.
d)
For a bag:
Suppose a tuple t occurs n and m times in R and S respectively. The union of these two bags in
the bag R ∪ S, tuple t would appear n + m times. The union of these two bags in the bag S ∪ R
tuple t would appear m + n times. Both sides of the relation yield the same result.
For a set:
In a set a tuple can only appear at most one time. Tuple t might appear in set R and S 1 or
0 times. The combinations of number of occurrences for tuple t in R and S respectively are
(0, 0), (0, 1), (1, 0), and (1, 1). The union of either one of these combinations on the right or left
side of the relation would yield the same result.
e)
For a bag:
Suppose a tuple t occurs n and m times in R and S respectively. The intersection of these two
bags in the bag R ∩ S, tuple t would appear min(n, m) times. The intersection of these two bags
in the bag S ∩ R tuple t would appear min(n, m) times. Both sides of the relation yield the same
result.
For a set:
In a set a tuple can only appear at most one time. Tuple t might appear in set R and S 1 or
0 times. The combinations of number of occurrences for tuple t in R and S respectively are
(0, 0), (0, 1), (1, 0), and (1, 1). The intersection of either one of these combinations on the right
or left side of the relation would yield the same result.
f)
We let R = {a, b}, S = {b, c}.
g)
31
On left side we have projection and on the right we have two projections. Union of the right side
is the same result as the left side. They have same condition (L) to project.
h)
For a bag:
Let R = {x}, S = {x}, T = {x}
For a set:
From set theory we can know that it holds for set.
i)
Does not hold for sets and bags. Left side when the query executes can not be the same with the
right side. On right side we have two expressions and those two can have some common tuples,
nothing else. Let’s take into account that conditions C and D are not the same.
Exercise 1.2
a)
Let R = {x}, S = {x, x}, T = {x}
b)
Let R = {x}, S = {x, x}, T = {x}
c)
It holds for sets because selection on the left side with condition OR will give the same result
with the union and situation on the right side.
Bags are unordered collection of elements with elements. Because of that, bags behave differ-
ently. And this example does not hold for bags.
a)
32
πA+B,A2 ,B 2 (R) = {(1, 0, 1), (5, 4, 9), (1, 0, 1), (6, 4, 16), (7, 9, 16)}
b)
πB+1,C−1 (S) = {(1, 0), (3, 3), (3, 4), (4, 3), (1, 1), (4, 3)}
c)
τB,A (R) = {(0, 1), (0, 1), (2, 3), (2, 4), (3, 4)}
d)
τB,C (S) = {(0, 1), (0, 2), (2, 4), (2, 5), (3, 4), (3, 4)}
e)
f)
δ(S) = {(0, 1), (2, 4), (2, 5), (3, 4), (0, 2)}
g)
h)
i)
j)
R ▷◁ S = {(2, 3, 4)}
γA,MAX(C) (R ▷◁ S) = {(2, 4)}
k)
◦
R ▷◁L S = {(0, 1, ⊥), (0, 1, ⊥), (2, 3, 4), (2, 4, ⊥), (3, 4, ⊥)}
l)
33
◦
R ▷◁R S = {(⊥, 0, 1), (⊥, 2, 4), (⊥, 2, 5), (2, 3, 4), (⊥, 0, 2), (⊥, 3, 4)}
m)
Combine above k) and l)
n)
◦
R ▷◁R.B<S.B S = {(0, 1, 2, 4), (0, 1, 2, 5), (0, 1, 3, 4), (0, 1, 3, 4),
(0, 1, 2, 4), (0, 1, 2, 5), (0, 1, 3, 4), (0, 1, 3, 4),
(2, 3, ⊥, ⊥), (2, 4, ⊥, ⊥), (3, 4, ⊥, ⊥),
(⊥, ⊥, 0, 1), (⊥, ⊥, 0, 2)}
Exercise 2.2
a)
We use R to represent the relation. For δ(R), there is no duplicates. So for δ(δ(R)), the effect
is none. So δ(R) = δ(δ(R)). And it is idempotent
b)
It is idempotent, because when we repeat the projection it will yield the same relation.
c)
The result of σC (R) is a relation where meets C. Repeating the selection will yield the same
results because the relation already satisfy C. So it is idempotent.
d)
The result is a relation whose schema consists of the grouping attributes and the aggregated
attributes. It is not idempotent.
e)
The result is sorted list of tuples based on some attributes L. It is not idempotent.
Exercise 2.3
R1 := ρA1,B1 (R)
R2 := R1 ▷◁A=A1 R
R3 := πA,A1 (R2)
34
5.3 A Logic for Relations
Exercise 3.1
Example of the Datalog rule which is finite with the head relation: Datalog rule we have:
R(x, y) AND z = z. The expression here is z = z. The actual datalog rule are the head
predicate has finite relation if the predicates of relational sub goals have finite relation. The
head relation for the above example is finite and hence it violates the condition. This violation
is irrespective of the finite relations that are assigned to the relational predicates.
a)
U (a, b, c) ← R(a, b, c)
U (a, b, c) ← S(a, b, c)
b)
c)
d)
e)
f)
P (a, b) ← R(a, b, c)
g)
35
X(a, b) ← R(a, b, c)
Y (b, c) ← S(a, b, c)
Y (a, b) ← Y (b, c)
Z(a, b) ← X(a, b) AND Y (a, b)
Exercise 4.2
a)
b)
c)
d)
e)
f)
Exercise 4.3
a)
b)
36
J(b, c, d, e) ← S(b, c, d) AND T (d, e)
c)
Exercise 4.4
Well, the process is the same as Exercise 4.2 above. Just add an extra AND operation. Omit
here.
Exercise 4.5
a)
πx,y (Q ▷◁ R)
b)
πx,y (Q)
c)
πx,y (Q ▷◁x<y R)
1. If A and B are two different attributes, there must be comma between them.
2. Because of that, B is an alias for A, because B is the second name, if it was that the second
name was A, in that case A would be an alias for B. Of course, we count that between
them there is no punctuation.
Exercise 1.2
a)
1 SELECT address
2 FROM Studio
3 WHERE name = 'MGM studios';
37
b)
1 SELECT birthdate
2 FROM MovieStar
3 WHERE name = 'Sandra Bullock';
c)
If you interpret the question as asking only that Lover appear as a substring, the the following
is OK:
1 SELECT starName
2 FROM StarsIn
3 WHERE movieYear = 1980 OR movieTitle LIKE '%Love%';
However, another reasonable interpretation is that we want the word Love as a word by itself.
The query above returns stars of a Movie The Cook, the Thief, His Wife, and Her Lover. To
identify only titles that have Lover as a word by itself, either at the beginning, the middle, or
the end as the entire title, we need to use four patterns. The following query works;
1 SELECT starName
2 FROM StarsIn
3 WHERE movieYear = 1980 OR
4 movieTitle LIKE 'Love %' OR
5 movieTitle LIKE '% Love %' OR
6 movieTitle LIKE '% Love' OR
7 movieTitle = 'Love';
d)
1 SELECT cert#
2 FROM MovieExec
3 WHERE netWorth >= 10000000
e)
This example is the same as c).
1 SELECT name
2 FROM MovieStar
3 WHERE gender = 'M' OR
4 address LIKE 'Malibu %' OR
5 address LIKE '% Malibu %' OR
6 address LIKE '% Malibu' OR
7 address = 'Malibu';
Exercise 1.3
38
d) Any tuple with a equal to b except NULL.
e) Any tuple with a ≤ b except NULL.
Exercise 1.4
It’s simple.
1 SELECT *
2 FROM Movies
3 WHERE length > 0;
a)
1 SELECT name
2 FROM StarsIn, MovieStar
3 WHERE gender = 'M' AND name = starName
4 AND movieTitle = 'Titanic';
b)
1 SELECT starName
2 FROM StarsIn, Movies
3 WHERE gender = 'M' AND name = starName
4 AND year = 1995
5 AND studioName = 'MGM';
c)
1 SELECT presC#
2 FROM Studio, MovieExec
3 WHERE Studio.name = 'MGM' AND presC# = cert#
d)
1 SELECT Movie1.title
2 FROM Movies Movie1, Movies Movie2
3 WHERE Movie2.title = 'Gone With the Wind'
4 AND Movie1.length > Movie2.length
e)
1 SELECT Exec1.name
2 FROM MovieExec Exec1, MovieExec Exec2
3 WHERE Exec2.name = 'Merv Griffin'
4 AND Exec1.netWorth > Exec2.netWorth
39
Exercise 2.2
A systematic way to handle this problem is to create a tuple variable for every Ri , i = 1, 2, . . . , n,
whether we need to or not. That is, the FROM clause is
1 FROM R1 AS T1, R2 AS T2, ...
Now, build the WHERE clause from C by replacing every reference to some attribute A of Ri by
Ti .A. Also, build the SELECT clause from list of attributes L by replacing every attribute A of
Ri by Ti .A.
Exercise 2.3
1 SELECT L
2 FROM R1 NATURAL JOIN R2, ...
3 ON R1.column = R2.column AND ...
4 WHERE C;
6.3 Subqueries
Exercise 3.1
1 SELECT title
2 FROM Movies Old
3 WHERE Movies.year < Old.year AND Movies.title = Old.title;
Exercise 3.2
1 SELECT *
2 (SELECT R1 FROM L) AS a
3 (SELECT R2 FROM L) AS b
4 ...
5 FROM L;
Exercise 3.3
a)
1 SELECT name, address
2 FROM MovieStar
3 WHERE gender = 'F' AND (name address) IN
4 (SELECT name, address
5 FROM MovieExec
6 WHERE netWorth > 1 100000000);
b)
40
1 SELECT name, address
2 FROM MovieStar
3 WHERE (name address) NOT IN
4 (SELECT name, address
5 FROM MovieExec);
Exercise 3.4
Exercise 3.5
a)
• Studio.name
• Studio.address
• presC#
• MovieExec.name
• MovieExec.address
• cert#
• netWorth
b)
• movieTitle
• movieYear
• starName
• name
• ADDRESS
• gender
• birthdate
c)
• movieTitle
• movieYear
• starName
41
• name
• ADDRESS
• gender
• birthdate
Exercise 3.6
1 SELECT *
2 FROM (PC FULL NATURAL OUTER JOIN Laptop
3 FULL NATURAL JOIN Printer) AS allProduct
4 LEFT OUTER JOIN Product ON Product.model = allProduct.model
Exercise 3.7
a)
1 SELECT *
2 FROM R.key = S.key
b)
1 SELECT *
2 FROM R, S
c)
1 SELECT *
2 FROM R, S
3 where C
Need help
Exercise 4.2
Need help
Exercise 4.3
42
Exercise 4.4
It is possible! In the γ, we need to produce all the aggregations that the HAVING clause uses.
Then, we can follow the γ by a σ that eliminated from the result of the γ the tuples that
correspond to the groups that the HAVING would eliminate. Finally, we use π to get rid of the
extra aggregations that were used only by the HAVING clause.
a)
1 SET TRANSACTION READ ONLY
2 ISOLATION LEVEL READ COMMITTED
3 BEGIN TRANSACTION
4 SELECT model, price
5 FROM PC
6 WHERE speed = providedSpeed, ram = providedRAM
7 COMMIT
b)
1 SET TRANSACTION READ WRITE
2 ISOLATION LEVEL SERIALIZABLE
3 BEGIN TRANSACTION
4 DELETE FROM PC
5 WHERE PC.model = providedModel
6 DELETE FROM Product
7 WHERE Product.model = providedModel
8 COMMIT
c)
1 SET TRANSACTION READ WRITE
2 ISOLATION LEVEL SERIALIZABLE
3 BEGIN TRANSACTION
4 UPDATE PC
5 SET price = price - 100
6 WHERE PC.model = providedModel
7 COMMIT
d)
1 SET TRANSACTION READ WRITE
2 ISOLATION LEVEL SERIALIZABLE
43
3 BEGIN TRANSACTION
4 UPDATE PC AS P
5 SET (maker, model, speed, ram, hd, price)
6 WHERE IF(maker=P.maker AND model=P.model AND speed=P.speed AND
7 ram=P.ram AND hd=P.hd AND price=P.price)
8 PRINT 'Error. There is model like that.'
9 COMMIT
10 COMMIT
Exercise 6.2
Here, I only give the answer for the first problem, it is easy to give an example for other situations.
When looking up the PC, we may UPDATE it at the same time.
Exercise 6.3
Here, I only give the answer for the first problem, it is easy to give an example for other situations.
When the ISOLATION LEVEL is READ UNCOMMITTED, we may look up the dirty data.
Exercise 6.4
• Serializable: With a lock-based concurrency control serializability requires read and write
locks to be released at the end of the transaction.When using non-lock based concurrency
control, no locks are acquired; however, if the system detects a write collision among several
concurrent transactions, only one of them is allowed to commit. Here the Transaction T,has
to acquire locks before reading and writing. So, there will be no impact of other processes
running.
• Repeatable reads: With a lock-based concurrency control this keeps read and write locks
until the end of the transaction. Write skew is possible at this isolation level, a situation
where two writes are allowed to the same column in a row by two different writers, resulting
in the row having data that is a mix of the two transactions. Here the Transaction T, has
to acquire locks before reading and writing. So, there will be no impact of other processes
running.
• Read committed: A lock-based concurrency control this keeps write locks until the end of
the transaction, but read locks are released as soon as the SELECT operation is performed.
Read committed is an isolation level that guarantees that any data read is committed
at the moment it is read. It simply restricts the reader from seeing any intermediate,
uncommitted, ’dirty’ read. Here the data read by Transaction T will always be committed
so there should be no issue with other process.
• Read uncommitted: This is the lowest isolation level. In this level, dirty reads are allowed,
so one transaction may see not-yet-committed changes made by other transactions. Here
it could be a situation where data updated by other transactions are not available and the
Transaction T keeps running for ever
44
Chapter 7 Constraints and Triggers
a)
1 FOREIGN KEY (producerC#) REFERENCES MovieExec(cert#);
b)
1 FOREIGN KEY (producerC#) REFERENCES MovieExec(cert#)
2 ON UPDATE SET NULL -- if MOvie exec cert# is changed, set to null
3 -- in practice this should not be used
4 -- since it will lose referential integrity
5 -- ideally, it should be CASCADE
6 ON DELETE SET NULL; -- if Movie exec does not exist
c)
1 FOREIGN KEY (producerC#) REFERENCES MovieExec(cert#)
2 ON UPDATE CASCADE
3 ON DELETE CASCADE;
d)
1 FOREIGN KEY (movieTitle, movieYear)
2 REFERENCES Movies(title, year);
3 -- by default, UPDATE and CASCADE are restrict
e)
1 FOREIGN KEY (starName) REFERENCES MovieStar(name)
2 ON DELETE CASCADE;
Exercise 1.2
No.
Assume the foreign key constraint (a) in relation R that references relation S.
Thus:
Therefore, we can’t enforce that every Movie has a tuple in StarsIn because:
45
Exercise 1.3
• Product: model
• PC: model
• Laptop: model
• Printer: model
The answers below are attribute constraints, defined after the corresponding <type> of the
attribute.
a)
1 year <type> CHECK (year >= 1915)
b)
1 length <type> CHECK (length >= 60 AND length <= 250)
c)
1 studioName <type> CHECK
2 (studioName in('Disney', 'Fox', 'MGM', 'Paramount'))
46
Exercise 2.2
The answers below are attribute constraints, defined after the corresponding <type> of the
attribute.
a) In relation Laptop:
1 speed <type> CHECK (speed >= 2.0 )
b) In relation Printer:
1 type <type> CHECK (type IN ('laser','ink-jet', 'bubble-jet'))
c) In relation Product:
1 type <type> CHECK (type IN ('PC','laptop', 'printer'))
d) In relation Product. This checks that it exists in at least one of them, not that it exists in
exactly one of them. This is one potential solution:
1 model <type> CHECK (EXISTS (select model from Printers P where
2 P.model = model)) or
3 EXISTS (select model from Laptops L where
4 L.model = model)) or
5 EXISTS (select model from Printers P where
6 P.model = model)))
Exercise 2.3
c) as a tuple constraints:
For MovieStar:
1 CHECK (name NOT IN (SELECT name FROM MovieExec))
47
For MovieExec:
1 CHECK (name NOT IN (SELECT name FROM MovieStar))
Exercise 2.4
Exercise 2.5
a)
1 CHECK(bore > 16)
b)
1 CHECK(numGuns < 9 -- at most 9 guns
2 OR
3 bore < 14 ) -- or its bore must be no longer than 14
c)
In Ships:
48
1 CHECK(launched < ALL (SELECT date
2 FROM Battles B JOIN Outcomes
3 ON (battle = B.name
4 and ship = name))
Exercise 2.5
a)
1 ALTER TABLE Movie ADD CONSTRAINT MovieKey
2 PRIMARY KEY(title, year);
b)
1 ALTER TABLE MovieExec ADD CONSTRAINT P1
2 CHECK(name IN (SELECT producerC# FROM Movie));
c)
1 ALTER TABLE Movie ADD CONSTRAINT P2
2 CHECK(length >= 60 AND length <= 250);
d)
1 ALTER TABLE MovieStar ADD CONSTRAINT P3
2 CHECK(name NOT IN (SELECT name FROM MovieExec));
3 ALTER TABLE MovieExec ADD CONSTRAINT P4
4 CHECK(name NOT IN (SELECT name FROM MovieStar));
e)
1 ALTER TABLE Studio ADD CONSTRAINT P5
2 UNIQUE(address);
Exercise 3.2
a)
1 ALTER TABLE Classes ADD CONSTRAINT P1
2 PRIMARY KEY(class, country);
b)
49
1 ALTER TABLE Battles ADD CONSTRAINT P2
2 CHECK(name IN (SELECT battle FROM Outcomes));
c)
1 ALTER TABLE Ships ADD CONSTRAINT P3
2 CHECK(name in (SELECT ship FROM Outcomes));
d)
1 ALTER TABLE Classes ADD CONSTRAINT P4
2 CHECK(numGuns <= 14);
e)
1 ALTER TABLE Battle ADD CONSTRAINT P5
2 CHECK(date NOT IN (SELECT launched FROM Ships));
7.4 Assertions
Exercise 4.1
a)
1 CREATE ASSERTION no_maker CHECK(
2 NOT IN ((SELECT maker FROM Product NATURAL JOIN PC)
3 INTERSECT
4 (SELECT maker FROM Product NATURAL JOIN Laptop))
5 );
b)
1 CREATE ASSERTION speed_better CHECK(
2 NOT EXISTS (PC JOIN Laptop ON PC.model = Laptop.model AND
3 Laptop.speed < PC.speed )
4 );
c)
1 CREATE ASSERTION more_memory_more_price CHECK(
2 NOT EXISTS (PC JOIN Laptop ON PC.model = Laptop.model AND
3 Laptop.ram > PC.ram AND
4 Laptop.price <= PC.price )
5 );
d)
1 CREATE ASSERTION check_integrity CHECK(
2 NOT EXISTS((SELECT model FROM PC)
3 INTERSECT
4 (SELECT model FROM Laptop)
50
5 UNION
6 ((SELECT model FROM Laptop)
7 INTERSECT
8 (SELECT model FROM Printer)
9 )
10 );
Exercise 4.2
a)
1 CREATE ASSERTION class_no_more_than_2_sheep CHECK(
2 NOT EXISTS (2 >= ALL (SELECT COUNT(*)
3 FROM Ships
4 GROUP BY class)
5 )
b)
1 CREATE ASSERTION noCountry CHECK(NOT EXISTS (
2 (SELECT type FROM Classes WHERE type=bb)
3 INTERSECT
4 (SELECT type FROM Classes WHERE type=bc))
5 );
c)
1 CREATE ASSERTION sunkCheck CHECK(
2 SELECT *
3 FROM ((Classes NATURAL JOIN Ships) NATURAL JOIN
4 Battles WHERE numGuns > 9)
5 INTERSECT
6 ((SELECT * FROM Classes NATURAL JOIN
7 Ships WHERE numGuns < 9) NATURAL JOIN Battles WHERE result
='sunk')
8 );
d)
1 CREATE ASSERTION launched CHECK(
2 NOT IN(SELECT class
3 FROM Ships
4 WHERE class='Tennessee')
5 INTERSECT
6 (1921 > ALL (SELECT launched FROM class))
7 );
e)
51
1 CREATE ASSERTION nameShip CHECK(
2 SELECT name, class
3 FROM Ships S, Classes C
4 WHERE S.name=C.class
5 );
Exercise 4.3
7.5 Triggers
Exercise 5.1
Exercise 5.2
a)
52
1 CREATE TRIGGER noLowerPrice
2 AFTER UPDATE OF price ON PC
3 REFERENCING
4 OLD AS OldTuple
5 NEW AS NewTuple
6 FOR EACH ROW
7 WHEN (OldTuple.price << NewTuple.price)
8 UPDATE PC
9 SET price = OldTuple.price
10 WHERE speed = OldTuple.speed;
b)
1 CREATE TRIGGER NewPrinterTrigger
2 AFTER INSERT ON Printer
3 REFERENCING
4 NEW ROW AS NewRow,
5 NEW TABLE AS NewStuff
6 FOR EACH ROW
7 WHEN (NOT EXISTS (SELECT * FROM Product
8 WHERE Product.model = NewRow.model))
9 DELETE FROM Printer
10 WHERE (model, color, type, price) IN NewStuff;
c)
1 CREATE TRIGGER averagePrice
2 INSTEAD OF
3 UPDATE OF price OF Laptop
4 INSERT ON Laptop
5 DELETE ON Laptop
6 REFERENCING
7 OLD_TABLE AS OldStuff
8 NEW_TABLE AS NewStuff
9 WHEN(1500 <= ALL (
10 SELECT AVG(price)
11 FROM (Laptop EXCEPT OldStuff) UNION NewStuff, Product
12 WHERE Laptop.model = Product.model
13 GROUP BY maker ))
14 DELETE FROM Laptop
15 WHERE (model, speed, ram, hd, screen, price) IN OldStuff
16 INSERT INTO Laptop
17 (SELECT * FROM NewStuff);
d)
1 BEFORE UPDATE ON PC FOR EACH ROW
2 BEGIN
3 IF NEW.hd $>=$ 100*(NEW.ram)
4 THEN
53
5 SIGNAL SQLSTATE '45000'
6 SET MESSAGE\_TEXT = 'Cannot add or update row: HardDisk
7 should be at least 100 times more than RAM
.';
8 END IF;
9 END;
e)
1 BEFORE INSERT ON PRODUCT
2 BEGIN
3 IF NEW.model IN (SELECT DISTINCT model FROM PC
4 UNION ALL
5 SELECT DISTINCT model FROM Printer
6 UNION ALL
7 SELECT DISTINCT model FROM Laptop)
8 THEN
9 SIGNAL SQLSTATE '45000'
10 SET MESSAGE\_TEXT = 'Cannot add or update row: Model number
already assigned.
11 Please try with another model number';
12 END IF;
13 END;
Exercise 5.3
Need help
Exercise 5.4
Need help
a)
1 CREATE VIEW RichExec AS
2 SELECT name, address, cert#, netWorth
3 FROM MovieExec
4 WHERE netWorth >= 10000000;
b)
54
1 CREATE VIEW StudioPres AS
2 SELECT name, address, cert#
3 FROM MovieExec, Studio
4 WHERE cert# = presC#
c)
1 CREATE VIEW
2 ExecutiveStar(name, address, gender, birthdate, cert#, netWorth) AS
3 SELECT star.name, star.address, star.gender, start.birthdate,
4 exec.cert#, exec.netWorth
5 FROM MovieStar star, MovieExec exec
6 WHERE star.name = exec.name AND star.address = exec.address
Exercise 1.2
a)
1 SELECT name
2 FROM ExecutiveStar
3 WHERE gender = 'F'
b)
1 SELECT RichExec.name
2 FROM RichExec, StudioPres
3 WHERE RichExec.name = StudioPres.name
c)
1 SELECT ExecutiveStar.name
2 FROM ExecutiveStar, StudioPres
3 WHERE ExecutiveStar.netWorth >= 50000000 AND
4 ExecutiveStar.name = StudioPres.name
a) is updatable because it has only one table in the FROM clause. Other two queries are not
because they save two tables in FROM clause.
Exercise 2.2
55
3 REFERENCING NEW ROW AS NewRow
4 FOR EACH ROW
5 INSERT INTO Movies(title, year, length, studioName, genre)
6 VALUES(NewRow.title, NewRow.year, NewRow.length,
7 'Disney', 'comedy');
c)
1 CREATE TRIGGER DisneyComedyUpdate
2 INSTEAD OF UPDATE ON DisneyComedies
3 REFERENCING NEW ROW AS NewRow
4 FOR EACH ROW
5 UPDATE Movies SET length NewRow.length
6 where title = NewRow.title AND year = NewRow.year
7 AND studioName = 'Disney' AND genre = 'comedy';
Exercise 2.3
c)
1 CREATE TRIGGER NewPCUpdatePrice
2 INSTEAD OF UPDATE ON NewPC
3 REFERENCING NEW ROW AS NewRow
4 FOR EACH ROW
5 UPDATE PC SET price NewRow.price
6 WHERE model = NewRow.model
d)
1 CREATE TRIGGER NewPCDelete
2 INSTEAD OF DELETE ON NewPC
3 REFERENCING OLD ROW AS OldRow
4 FOR EACH ROW
5 DELETE FROM Product WHERE model = OldRow.model
6 DELETE FROM PC WHERE model = OldRow.model
56
8.3 Indexes in SQL
Exercise 3.1
a)
1 CREATE INDEX StudioNameIndex ON Studio(name);
b)
1 CREATE INDEX MovieExecAddress ON MovieExec(address);
c)
1 CREATE INDEX MovieInfoIndex on Movies(genre, length);
Exercise 4.2
Exercise 5.1
Exercise 5.2
Exercise 5.3
Exercise 5.4
57
9.2 The SQL Environment
There are no questions in this section.
a)
1 int main() {
2 EXEC SQL BEGIN DECLARE SECTION;
3 char maker[10];
4 int model;
5 int speed;
6 int price;
7 EXEC SQL END DECLARE SECTION;
8 EXEC SQL WHENEVER SQLERROR GOTO query_error;
9 EXEC SQL WHENEVER NOT FOUNT GOTO bad_number;
10 printf("Enter the price for PC:"); scanf_s("%d", &price);
11 EXEC SQL DECLARE c CURSOR FOR EXEC SQL SELECT Product.maker,
12 Product.model,
13 Product.speed FROM Product,
14 PC WHERE Product.model = PC.model ORDER BY abs(Product.model - PC
.model) EXEC SQL OPEN CURSOR c;
15 while (1) {
16 EXEC SQL FETCH c INTO: maker,
17 :model,
18 :speed;
19 if (NOT FOUND) break;
20 }
21 EXCEL SQL CLOSE CURSOR c;
22 query_error;
23 printf("SQL error: %ld", sqlca->sqlcode);
24 exit();
25 bad_number;
26 printf("Invalid order number");
27 exit()
28 }
b)
1 int main() {
2 EXEC SQL BEGIN DECLARE SECTION;
3 char maker[10];
4 int model;
5 int speed;
6 int ram;
7 int hd;
58
8 int screen;
9 int price;
10 EXEC SQL END DECLARE SECTION;
11 EXEC SQL WHENEVER SQLERROR GOTO query_error;
12 EXEC SQL WHENEVER NOT FOUND GOTO bad_number;
13 printf("Enter the minimum Speed, RAM, Hard disk, Screen size of
the PC");
14 scanf_s("%d %d %d %d, &price, &ram, &hd, &speed");
15 EXEC SQL DECLARE c CURSOR FOR EXEC SQL SELECT Product.maker,
16 Laptop.model,
17 Laptop.speed,
18 Laptop.ram,
19 Laptop.hd,
20 Laptop.price,
21 Laptop.screen FROM Product,
22 PC WHERE PC.model = Laptop.model ORDER BY abs(PC.price - Laptop.
price);
23 EXEC SQL OPEN CURSOR c;
24 while (1) {
25 EXEC SQL FETCH c INTO: maker,
26 :model,
27 :speed,
28 :ram,
29 :hd,
30 :price,
31 :screen;
32 IF(NOT FOUND) break
33 }
34 EXEC SQL CLOSE CURSOR c;
35 query_error;
36 printf("SQL error: % ld ", sqlca->sqlcode);
37 exit();
38 bad_number;
39 printf("Invalid order number ");
40 exit()
41 }
c)
1 int main() {
2 EXEC SQL BEGIN DECLARE SECTION;
3 char maker[10];
4 int model;
5 int speed;
6 int ram;
7 int hd;
8 int screen;
9 int price;
10 EXEC SQL END DECLARE SECTION;
59
11 EXEC SQL WHENEVER SQLERROR GOTO query_error;
12 EXEC SQL WHENEVER NOT FOUND GOTO bad_number;
13 printf("Enter the manufacturer of the PC");
14 scanf_s("%s, &maker");
15 EXEC SQL DECLARE c CURSOR FOR EXEC SQL SELECT Laptop.model,
16 Product.type,
17 Laptop.screen,
18 Laptop.ram,
19 Laptop.hd,
20 Laptop.price,
21 Laptop.screen FROM Product,
22 PC,
23 Laptop WHERE Product.model = Pc.model AND Laptop.model;
24 EXEC SQL OPEN CURSOR c;
25 while (1) {
26 EXEC SQL FETCH c INTO: model,
27 :type,
28 :speed,
29 :ram,
30 :hd,
31 :price,
32 :screen;
33 IF(NOT FOUND) break
34 }
35 EXEC SQL CLOSE CURSOR c;
36 query_error;
37 printf("SQL error: % ld ", sqlca ->sqlcode);
38 exit();
39 bad_number;
40 printf("Invalid order number ");
41 exit()
42 }
d)
1 int main() {
2 EXEC SQL BEGIN DECLARE SECTION;
3 char maker[10];
4 int model;
5 int pmodel;
6 int speed;
7 int price;
8 EXEC SQL END DECLARE SECTION EXEC SQL WHENEVER SQLERROR GOTO
query_error;
9 EXEC SQL WHENEVER NOT FOUND GOTO bad_number;
10 printf("Enter the budget, minimum speed of the PC:");
11 scanf_s("%d %d, &price, &speed");
12 EXEC SQL DECLARE c CURSOR FOR EXEC SQL SELECT Laptop.model,
Printer.model FROM Product, Laptop, Printer, PC WHERE PC.model
60
<=<=price;
13 EXEC SQL OPEN CURSOR c;
14 while (1) {
15 EXEC SQL FETCH c INTO :model, :pmodel;
16 if(NOT FOUND) break;
17 }
18 EXCEL SQL CLOSE CURSOR c;
19 query_error;
20 printf("SQL error: %ld", sqlca->sqlcode);
21 exit();
22 bad_number;
23 printf("Invalid order number");
24 exit();
25 }
e)
1 int main() {
2 EXEC SQL BEGIN DECLARE SECTION;
3 char maker[10];
4 int model;
5 int speed;
6 int ram;
7 int hd;
8 int price;
9 EXEC SQL END DECLARE SECTION EXEC SQL WHENEVER SQLERROR GOTO
query_error;
10 EXEC SQL WHENEVER NOT FOUND GOTO bad_number;
11 printf("Enter the manufacturer, speed, ram, hard disk, price of
the PC: ");
12 scanf_s("%s %d %d %d %d %d, &maker, &model, &speed, &ram, &hd, &
price");
13 EXEC SQL DECLARE c CURSOR FOR EXEC SQL SELECT Product.maker,
Laptop.model, Laptop.speed, Laptop.ram, Laptop.hd, Laptop.price
FROM Product, PC, Laptop WHERE PC.model=model;
14 EXEC SQL OPEN CURSOR c;
15 while (1) {
16 EXEC SQL FETCH c INSERT INTO Product (maker, model, type)
VALUES (:maker, :model, :") INSERT INTO PC (model, speed, ram, hd,
screen, price) VALUES (:model, :speed, :ram, :hd:", :price);
17 if(NOT FOUND) bad_order;
18 }
19 EXCEL SQL CLOSE CURSOR c;
20 query_error;
21 printf("SQL error: %ld", sqlca->sqlcode);
22 exit();
23 bad_number;
24 printf("Invalid order number");
61
25 exit();
26 }
Exercise 3.2
a)
1 int main() {
2 EXEC SQL BEGIN DECLARE SECTION;
3 int bore;
4 int numGuns;
5 EXEC SQL END DECLARE SECTION;
6 EXEC SQL WHENEVER SQLERROR GOTO query_error;
7 EXEC SQL WHENEVER NOT FOUND GOTO bad_number;
8 printf("Enter the bore and number of Guns");
9 scanf_s("%d%d, &bore, &numGuns");
10 EXEC SQL DECLARE c CURSOR FOR EXEC SQL SELECT R1 AS SELECT (
numGuns*bore^3) AS firepower FROM Classes, R2 AS SELECT min(
firepower) minimum FROM R1, SELECT Classes FROM R1 WHERE firepower
= (SELECT minimum FROM R2);
11 EXEC SQL OPEN CURSOR c;
12 while(1){
13 EXEC SQL FETCH c INTO :bore, :numGuns;
14 IF(NOT FOUND) break
15 }
16 EXEC SQL CLOSE CURSOS c;
17 query_error;
18 printf("SQL error: "%ld, sqlca->sqlcode");
19 exit();
20 bad_number;
21 printf("Invalid order number");
22 exit();
23 }
b)
1 int main() {
2 EXEC SQL BEGIN DECLARE SECTION;
3 string name;
4 string class;
5 string country;
6 string outcomes;
7 string battle;
8 EXEC SQL END DECLARE SECTION;
9 EXEC SQL WHENEVER SQLERROR GOTO query_error;
10 EXEC SQL WHENEVER NOT FOUND GOTO bad_number;
11 printf("Enter the name of battle");
12 scanf_s("%s, &name");
62
13 EXEC SQL DECLARE c CURSOR FOR EXEC SQL SELECT R1 AS SELECT class,
name AS ship, country FROM Classes NATURAL JOIN Ships, R2 AS
SELECT battle, result, country FROM Outcomes NATURAL JOIN R1, R3
AS (SELECT count(*) AS COUNT, country FROM R2 WHERE result=? AND
battle=? GROUP BY country) SELECT country FROM R3 WHERE COUNT="",
SELECT(max(COUNT) FROM R3);
14 EXEC SQL OPEN CURSOR c;
15 while (1) {
16 EXEC SQL FETCH c INTO name:, :class, :country, :outcomes, :
battle;
17 IF(NOT FOUND) break
18 }
19 EXEC SQL CLOSE CURSOS c;
20 query_error;
21 printf("SQL error: "%ld, sqlca->sqlcode");
22 exit();
23 bad_number;
24 printf("Invalid order number");
25 exit();
26 }
c)
1 int main() {
2 EXEC SQL BEGIN DECLARE SECTION;
3 int numGuns;
4 int bore;
5 int displacement;
6 string class;
7 string country;
8 string type;
9 string name;
10 string class;
11 int launched;
12 EXEC SQL END DECLARE SECTION;
13 EXEC SQL WHENEVER SQLERROR GOTO query_error;
14 EXEC SQL WHENEVER NOT FOUND GOTO bad_number;
15 printf("Enter the name of class and information about tuple of
Classes");
16 scanf_s("%s%s%s%d%d%d, &class, &country, &type, &numGuns, &bore,
&displacement");
17 printf("Enter the name, class and launched of the ships");
18 scanf_s("%s%s%d, &name,&class, &launched");
19 EXEC SQL DECLARE c CURSOR FOR EXEC SQL SELECT INSERT INTO Classes
VALUES(?,?,?,?,?,?), INSERT INTO Ships VALUES(?,?,?);
20 EXEC SQL OPEN CURSOR c;
21 while(1) {
22 EXEC SQL FETCH c INTO name:, :class, :country, :numGuns, :bore,
63
:displacement, :type, :name, :class, :launched;
23 IF(NOT FOUND) break
24 }
25 EXEC SQL CLOSE CURSOS c;
26 query_error;
27 printf("SQL error: "%ld, sqlca->sqlcode");
28 exit();
29 bad_number;
30 printf("Invalid order number");
31 exit();
32 }
d)
1 int main() {
2 EXEC SQL BEGIN DECLARE SECTION;
3 int launched;
4 string battle;
5 string name;
6 string date;
7 EXEC SQL END DECLARE SECTION;
8 EXEC SQL WHENEVER SQLERROR GOTO query_error;
9 EXEC SQL WHENEVER NOT FOUND GOTO bad_number;
10 printf("Enter the name of ships that were in battle before they
were launched");
11 scanf_s("%s%s%d%s, &name,&battle, &launched, &date");
12 EXEC SQL DECLARE c CURSOR FOR EXEC SQL SELECT SELECT name.s,
launched.s, date.b FROM Ships.s, Battles.b WHERE date<launced;
13 EXEC SQL OPEN CURSOR c;
14 while(1) {
15 EXEC SQL FETCH c INTO name:, :date, :name, :battle, :launched;
16 IF(NOT FOUND) break
17 }
18 EXEC SQL CLOSE CURSOS c;
19 query_error;
20 printf("SQL error: "%ld, sqlca->sqlcode");
21 exit();
22 bad_number;
23 printf("Invalid order number");
24 exit();
25 }
Exercise 4.1
a)
64
1 CREATE FUNCTION Movie (n CHAR(20), w INT) RETURN INT
2 SELECT name, netWorth
3 FROM Studio, MovieExec
4 WHERE name=n AND netWorth=w;
b)
1 CREATE FUNCTION nameAddress(n CHAR(20), a CHAR(20)) RETURN BOOLEAN
2 IF(SELECT name.S, name.E
3 FROM MovieStar.S, MovieExec.E
4 WHERE n=name.S AND NOT (n=name.E)) RETURN 1;
5 ELSEIF 2 <= (SELECT name.S,name.E
6 FROM MovieStar.S, MovieExec.E
7 WHERE n=name.E AND NOT (n=name.S) )
8 ELSEIF 3 <= (SELECT name.S, name.E
9 FROM MovieStar.S, MovieExec.E
10 WHERE n=name.E AND n=name.S)
11 ELSEIF 4 <= (SELECT name.S, name.E
12 FROM MovieStar.S, MovieExec.E
13 WHERE NOT(n=name.E) AND NOT(n=name.S))
14 END IF;
c)
1 CREATE FUNCTION sName (s CHAR(20)) RETURN CHAR
2 IF(SELECT *
3 FROM Movies
4 WHERE studioName=s
5 GROUP BY length
6 HAVING COUNT(*)=2)
7 THEN RETURN sName;
8 ELSE IF (SELECT *
9 FROM Movies
10 WHERE studioName=s
11 GROUP BY length
12 HAVING COUNT(*)=1)
13 THEN RETURN 'There is no second longest';
14 END IF;
d)
1 CREATE FUNCTION star (s CHAR(20)) RETURN INT
2 IF(SELECT MIN(year) AS lowestYear, length
3 FROM Movies
4 WHERE length>120)
5 RETURN s;
6 ELSE RETURN 0;
7 END IF;
e)
65
1 CREATE FUNCTION address (a CHAR(50)) RETURN CHAR
2 IF (SELECT name
3 FROM MovieStar
4 WHERE address=a
5 GROUP BY name
6 HAVING COUNT(address)=1)
7 RETURN a;
8 ELSEIF SELECT name
9 FROM MovieStar
10 WHERE address=a
11 GROUP BY name
12 HAVING COUNT(address)=0 OR HAVING COUNT(address)>1
13 RETURN NULL;
14 END IF;
f)
1 CREATE FUNCTION nameStar (n CHAR(50)) RETURN CHAR
2 (DELETE name FROM MovieStar WHERE n=name);
3 UNION (DELETE title, movieTitle
4 FROM StarsIn, Movies
5 WHERE n=starName AND n=title);
Exercise 4.2
a)
1 CREATE FUNCTION priceP (p INT) RETURN INT
2 IF(SELECT MIN(price)
3 FROM PC
4 WHERE price=p); RETURN INT;
5 END IF;
b)
1 CREATE FUNCTION productModel (m CHAR(20), n INT)
2 IF(SELECT price
3 FROM PC, Laptop, Printer
4 WHERE m=maker AND n=model); RETURN price;
5 END IF;
c)
1 CREATE FUNCTION takeALL (m INT, s INT, r INT, h INT, p INT ) RETURN
CHAR
2 INSERT INTO PC VALUES(m, s, r, h, p)
3 IF(SELECT model, speed, ram, hd, price
4 FROM PC
5 WHERE model=m, speed=s, ram=r, hd=h, price=p); RETURN 'Error';
6 ELSE model=model+1;
66
d)
1 CREATE FUNCTION morePrice (p INT) RETURN INT
2 IF(SELECT price
3 FROM Laptop, PC, Printer
4 WHERE p=price
5 HAVING COUNT(*)
6 GROUP BY Laptop, PC, Printer;) RETURN INT;
7 END IF;
Exercise 4.3
a)
1 CREATE FUNCTION firepower (c CHAR(50)) RETURN INT
2 IF(SELECT class, numGuns, bore
3 FROM Classes WHERE c=class); RETURN numGuns*bore^3;
b)
1 CREATE FUNCTION battleName (n CHAR(20)) RETURN
2 IF(SELECT name, country
3 FROM Battles
4 WHERE n=name
5 HAVING COUNT (*)=2
6 GROUP BY country); RETURN n;
7 ELSEIF(SELECT name, country
8 FROM Battles
9 WHERE n=name
10 HAVING COUNT(*)>2 OR HAVING COUNT(*)<2
11 GROUP BY country; RETURN NULL;
c)
1 CREATE FUNCTION takeALL (c CHAR(20), t CHAR(20), c CHAR(20), n INT,
b INT, d INT)
2 INSERT INTO Classes VALUES(c, t, c, n, b, d);
3 INSERT INTO Ships WHERE c=class;
d)
1 CREATE FUNCTION shipBattle (n CHAR(20))
2 IF(SELECT date.B, launched.S
3 FROM Battles.B, Ships.s
4 WHERE date<launched;) RETURN date=0, launched=0;
5 ENDIF;
67
Exercise 4.4
∑
n
1∑ 2
n
(xi − x̄2 /n) = (x − 2x̄xi + x̄2 )
i=1
n i=1 i
1 (∑ 2 ∑ ∑ )
= xi − 2x̄xi + x̄2
n
1 (∑ 2 )
= xi − 2x̄(nx̄) + nx̄2
n
1 (∑ 2 )
= xi − nx̄2
n
Exercise 5.1
Exercise 5.2
9.6 JDBC
This section needs community help.
Exercise 6.1
Exercise 6.2
9.7 PHP
This section needs community help.
Exercise 7.1
Exercise 7.2
Exercise 7.3
68
Exercise 1.1
Exercise 1.2
Exercise 1.3
Exercise 2.1
Exercise 2.2
Exercise 2.3
Exercise 2.4
Exercise 3.1
Exercise 4.1
Exercise 5.1
Exercise 5.2
Exercise 5.3
69
Exercise 6.1
Exercise 6.2
Exercise 7.1
Exercise 7.2
Exercise 7.3
Exercise 7.4
Exercise 7.5
Exercise 7.6
Exercise 7.7
Exercise 1.2
UML is structured data model and its schema dependent and is less flexible. On the other
side, semistructured data is more flexible than structured data but less. As far as scalability in
structured data it is very difficult to scale DB schema, on the other hand in semistructured data
it’s scaling is simpler than structured data. But essential difference between this two models is:
UML is based on Relational database table and semi-structured model is based on XML/RDF.
11.2 XML
Exercise 2.1
Exercise 2.2
70
1 <tuples>
2 <tuple id="Tuple 1"> ... </tuple>
3 <elements>
4 <element id="Element 1"> ... </element>
5 <element id="Element 2"> ... </element>
6 ...
7 </elements>
8 ...
9 </tuples>
Exercise 2.3
An element with no content is said to be empty. In XML, you can indicate an empty element
like this: or you can use an empty tag, like this (this sort of element syntax is called self-closing).
Exercise 2.4
Exercise 3.1
Exercise 3.2
Exercise 4.1
Exercise 4.2
Exercise 4.3
71
12.1 XPath
Exercise 1.1
Exercise 1.2
12.2 XQuery
Exercise 2.1
Exercise 2.2
Exercise 2.3
Exercise 2.4
Exercise 3.2
a)
106
18 log2 250
= 18
12
b)
18 log2 1000
= 15
12
c)
18 log2 1000
6
= 12
12
d)
72
250 × 25 ≈ 8TB
1 × 25 = 32GB
6 × 25 = 192GHz
Exercise 1.2
3600
3×2 18 = 4.82 × 1060 GHz
13.2 Disks
Exercise 2.1
a)
b)
c)
1 + 100000 × 0.0002 = 21
d)
e)
64 × 0.288◦ + 63 × 0.072◦
6× = 0.3828 ms
360◦
f)
g)
6/2 = 3 ms
73
Exercise 2.2
81922 2
2 + 57344
2
= 25600
65536
And then we could calculate:
Exercise 2.3
When the head of the disk is located at the first cylinder, average distance moved by head is
1 + 2 + 3 + · · · + 65535
65536
When the head of the disk is located at the second cylinder, average distance moved by head is:
1 + 1 + 2 + 3 + · · · + 65534
65536
Thus for any distance when the head is located at first cylinder all the sums are 0 and sum of
distances for all cylinders will be (1+2+3+· · ·+N −1). For any distance, when head is located at
second cylinder all the sums are 1 and sum distances for all cylinders will be (1+2+3+· · ·+N −2).
This expression is expressed as the following.
(x − 1)(x − 1 + 1) (N − x)(N − x + 1)
+
2N 2N
And we calculate the average:
1 ∑ (x − 1)x (N − x)(N − x + 1)
N
+
N x=1 2N 2N
1 ∑
N
= ((x − 1)x + (N − x)(N − x + 1))
2N 2 x=1
1 ∑ 2
N
= (2x − (2N + 2)x + n + N 2 )
2N 2 x=1
1 ∑
N ∑N
= (2 x 2
− (2N + 2) x + N 2 + N 3)
2N 2 x=1 x=1
N2 − 1
=
3N
74
Exercise 2.4
3 4
p(x) = + x
5 5
he average distance between two sectors:
∫ 1 ∫ 1
p(xp(y)|x − y|dxdy)
0 0
Exercise 2.5
From the Section 13.2, we can know that the average latency and transfer time is 3.8 and 0.5
a)
• Request 1: cylinder 8000, since the disk head already on cylinder 8000 there is no seek
time. The access time is 4.3ms. And the completion time is 4.3ms
• Request 2: cylinder 48000, the seek time is 1 + (0.00025 × 40000) = 11ms, And the com-
pletion time is 11 + 4.3 + 4.3 = 19.6ms
• Request 3: cylinder 40000, the seek time is 1 + (0.00025 × 8000) = 3ms And the completion
time is 3 + 4.3 + 19.6 = 26.9ms
• Request 4: cylinder 4000, the seek time is 1+(0.00025×36000) = 10ms And the completion
time is 10 + 4.3 + 26.9 = 41.3ms
b)
We need to only calculate the seek time.
• Request 1: cylinder 8000, since the disk head already on cylinder 8000 there is no seek
time. The completion time is 4.3ms
• Request 2: cylinder 48000, the seek time is 1+(0.00025×40000) = 11ms And the completion
time is 11 + 4.3 + 4.3 = 19.6ms
• Request 3: cylinder 4000, the seek time is 1 + (0.00025 × 44000) = 12ms
• And the completion time is 12 + 4.3 + 19.6 = 35.9ms
• Request 4: cylinder 40000, the seek time is 1+(0.00025×36000) = 10ms And the completion
time is 10 + 4.3 + 35.9 = 50.2ms
75
Exercise 3.2
Exercise 3.3
a)
Number of request we can say that will be n; Head travels across 65536 takes n + 16.38 ms. The
result in the rotational latency of 4.17n ms. The time required for transfer is 0.13 ms which in
turn gives the total transfer of 0.13n ms
b)
16.38
A − 5.3
c)
The waiting time for the request will be 0 when the request comes in right. In some cases, the
request comes only after the pass has started which is a worst case.
When the request gets its pass then the wait must get serviced. The wait time for service would
be 0. The request will wait for the service at 0 ms, in the best case.
16.38
− 0.065 ms
A − 5.3
76
Exercise 3.4
Think of the requests as a random sequence of the integers 1 through 4. This sequence can be
divided up into segments that do not contain two of the same integer, in a greedy way. For
example, the sequence 123142342431 would be divided into 123, 1423, 42, and 31. The disks are
serving all the requests from one segment, and each request is generated and starts at about the
same time. When a segment is finished, the waiting request begins, along with other requests for
other disks that are, by our assumption, generated almost immediately and thus finish at about
the same time.
The question is thus: if I choose numbers 1, 2, 3, and 4 at random, how many choices, on the
average, can I make without a repeat? The cases are:
• 1/4 of the time we get an immediate repeat on the second choice, and the length is therefore
1.
• (3/4)(1/2) = 3/8 of the time we get our first repeat at third number, and the length is 2.
• (3/4)(1/2)(3/4) = 9/32 of the time we get our first repeat at the fourth number, and our
length is 3.
• The remaining fraction of the time, 3/32, we get four different numbers, and our length is
4.
Exercise 3.5
The disk serves all requests from one segment and each of these requests are generated and
started at the same time. The waiting request begins when the segment is finished. This request
is raised along with other disks and finishes at the same time. We have a request of the random
sequence of integers 1 through k. Now we can choose the numbers at random to find the number
of choices without repeat.
1
• When the length is 1, the it takes k of the time for the immediate repeat on the second
choice.
• When the length is 2, the time will be 1
k × 2.
• When the length is k, the time will be 1
k ×k
77
1 1 1
+ 2 × + ··· + k ×
k k k
1
= (1 + 2 + · · · + k)
k
k × (k + 1) 1
=
2 k
k
=
2
a)
There is the odd number of 1’s so the parity bit is 1.
b)
There is the even number of 1’s so the parity bit is 1
c)
There is an odd number of 1’s, so the parity bit is 1
Exercise 4.2
a) 1 0
b) 0 0
c) 1 0
Exercise 4.3
1
× 0.04 = 3.65 ms
1095
Exercise 4.4
a)
To compute the mean time to failure, we can compute the probability that the system fails in a
given year. The MTTF will be the inverse of that probability. Note that there are 8760 hours in
a year. The system fails if the second disk fails while the first is being repaired. The probability
of a failure of one of the two disks in a year is 2F . The probability that the second disk will
fail during the H hours that the other is being prepared is F H/8760. Thus, the probability of a
failure in any year is 2F 2H/8760 , and the MTTF is 4380/F 2H .
b)
78
The system fails if any of the other N − 1 disks fails while the first is being repaired. The
probability of a failure of one of the N disks in a year is N F . The probability that a second
disk will fail during the H hours that the other is being prepared is (N − 1)F H/8760. Thus, the
probability of a failure in any year is N (N − 1)F 2H/8760 .
Exercise 4.5
87602
3F 3 H 2
Exercise 4.6
a) 01010110
b) 00110110
Exercise 4.7
a) 01010110
b) 00110110
Exercise 4.8
a) 10101010
b) 01101100
Exercise 4.9
a)
• 00111100
• 11000111
• 01010101
• 10000100
• 10101110
• 01111111
• 11101101
b)
• 00111100
• 00001111
79
• 01010101
• 10000100
• 01100110
• 10110111
• 11101101
Exercise 4.10
a)
(Row 1) Using disks 2, 3, and 5, recover disk 1 (Row 3) Using disks 1, 3, and 4, recover disk 7
b)
(Row 1) Using disks 2, 3, and 5, recover disk 1 (Row 2) Using disks 1, 2, and 6, recover disk 4
c)
(Row 1) Using disks 1, 2, and 5, recover disk 3 (Row 2) Using disks 1, 2, and 4, recover disk 6
a)
15 + 2 + 10 + 8 = 35 bytes
b)
c)
80
Exercise 5.2
a)
8 + 17 + 1 + 10 = 36 bytes
b)
c)
Exercise 5.3
4 + 4 + 1 + 35 = 44 bytes
b)
4 + 4 + (1 + 3) + 40 = 52 bytes
c)
(4 + 4) + (4 + 4) + (1 + 7) + 48 = 72bytes
Exercise 5.4
8 + 20 + 36 = 64 bytes
81
b)
8 + 20 + 48 = 76 bytes
c)
8 + (20 + 4) + 56 = 88bytes
Exercise 6.1
Exercise 6.2
Exercise 6.3
Exercise 6.4
Exercise 6.5
Exercise 6.6
Exercise 6.7
Exercise 6.8
Exercise 6.9
4 + (3 − 1) × 4 + 3 × 10 = 42 bytes
• 4: Record length.
Exercise 7.2
10 + 50 20 + 80 0 + 1000
2 + 40 + + + = 622 bytes
2 2 2
82
Exercise 7.3
Exercise 7.4
4 + (40 × n × p)
b)
4 + ((40 + 4) × n × p)
c)
40k + 4
d)
k =n×p
Exercise 7.5
Every record and record fragment requires a fragment header to support spanned records. Since
500 < r ≤ 1000, we can store only a single record when we don’t support spanned records.
Therefore, we don’t use 1000 − r bytes in this case. If we support them, we can store one
record and one record fragment and we need to use 32 bytes for fragment headers. Therefore, if
r < 1000 − 32 = 968, then we can improve space usage.
Exercise 7.6
8 × 256 × 4096
= 418
298.262 × 67.140125
If we organize 418 chunks from different movies fit into 8 tracks and all movies occupy consecutive
cylinders, we can play as many as 418 movies with an initial delay of 100 ms.
83
• Better space utilization. The variable-length tuples need allocation of space large enough
for handling the maximum possible size of tuples. Many tuples are much smaller and thus
storage space could be wasted. Thus, the fixed-length tuple is preferred over variable-length
tuple.
• Simple processing. It is easy to update, insert and delete fixed-length tuple.
a)
The number of blocks to store the record is:
n
3
The number of blocks to store the pointer is:
n
10
Thus, the answer is:
n n 13n
+ =
3 10 30
b)
The number of blocks to store the record is:
n
3
The number of blocks to store the pointer is:
n/3
10
Thus, the answer is:
n n/3 11n
+ =
3 10 30
84
Exercise 1.2
a)
The number of blocks to store the record is:
n
200 × 80%
The number of blocks to store the pointer is:
n
30 × 80%
Thus, the answer is:
n n 23n
+ =
200 × 80% 30 × 80% 480
b)
The number of blocks to store the record is:
n
30 × 80%
The number of blocks to store the pointer is:
n
30 × 80% × 200 × 80%
Thus, the answer is:
n n 161n
+ =
30 × 80% 30 × 80% × 200 × 80% 3840
14.2 B-Trees
Exercise 2.1
a)
The number to store the data is
85
14286/70 ≈ 205 blocks
205/70 ≈ 3 blocks
3/70 ≈ 1 blocks
Total blocks:
86
Exercise 2.2
Exercise 2.3
16384
= 4096
4
16384
= 1365
12
Exercise 2.4
a)
Interior nodes: at least 5 keys and 6 pointers. Leaves: at least 5 keys and pointers
b)
Interior nodes: at least 5 keys and 6 pointers. Leaves: at least 6 keys and pointers
87
15.2 One-Pass Algorithms
Exercise 2.1
Exercise 2.2
a)
This is not a blocking operator. The projection operation does not need entire relation to produce
the output.
b)
This is not a blocking operator. The distinct tuple is always in the memory.
c)
This is a blocking operator. To group we need to examine every tuple.
d)
This is not a blocking operator. Because we could read each block into the buffer once at a time.
e)
This is not a blocking operator. The operation does not require the entire tuple structure to
produce the output.
f)
This can be or can not be a blocking operator. If the computation starts from R to S then each
block of R is read and the operation is not blocking. If R is read into M − 1 blocks then each
block of S is read and the operation is blocking.
g)
This is not a blocking operator. This operation does not need all the tuples to produce the
output.
h)
This can be or can not be a blocking operator. This operation is the similar to the operation in
the set difference.
i)
This is not a blocking operator. This operation does not need all the tuples to produce the
output.
j)
This is not a blocking operator. It just need the matching tuple between the relations to produce
the output.
Exercise 2.3
Both arguments are not clustered which means we need to read every tuple.
88
Operators Memory required Disk I/O required
σ, Π 1 T
γ, δ 1 T
∩, ∪, −,× T (S)/T (B) T(S) + T(R)
Exercise 2.4
Exercise 3.2
B(S) + ((B(S)B(R)))/(M − 1)
We have
Exercise 3.3
89
B(S) + ((B(S)B(R)))/(M − 1) = IO
We can have:
M = (IO − B(S))/((B(S)B(R))) + 1
(a)
M = 1112
(b)
M = 6.667
(c)
M = 20.001
Exercise 3.4
a)
A block based nested loop join, used to join two relation R and S in a relational database, but
nested loop join, used to two relations and the outer and inner join respectively.
b)
case 1: R is not clustered and smaller. Cost of reading all tuples of S (clustered), T (S) + B(S)
Cost of reading R tuples and the cost of join with S in the main memory is: T (R)+B(S)B(R)/M
The total cost is T (R) + B(S)B(R)/M + T (S) + T (R)
case 2: S is not clustered and smaller. Cost of reading all tuples of S (clustered), T (R) + B(R)
Cost of reading S tuples and the cost of join with R in the main memory is: T (S)+B(S)B(R)/M
The total cost is T (S) + B(S)B(R)/M + T (R) + B(R)
Exercise 3.5
90
Exercise 4.2
(a)
(b)
(c)
Exercise 4.3
By using the extra buffers for saving disk I/O, we can also increase the overall performance of
I/O. Sometimes I/O is the bottleneck for the is against improvement of runtime performance.
That’s why, it has a big role in this system. But there are also some types of buffering of disk
I/O. They are single buffering, double buffering, circular buffer, an so on. A system transmits
data of I/O to one buffer while operating system empties the other in double buffering.
Exercise 4.4
Total number of disk I/O: sort relation R and S + (y-values ∗ number of I/O)
a)
b)
c)
Exercise 4.5
a)
b)
91
2(B(R) + B(S)) + (5 ∗ 500) = 5500
c)
Exercise 4.6
(a)
√
M= B = 100
(b)
√
M= B = 100
(c)
√
M= B(S)B(R) = 10000
Exercise 4.7
Exercise 4.8
Exercise 4.9
Since the merging operation is used for sorting the sequence hence the total time complexity is
O(M log(M )) and the space complexity is log(M ). The concerned term is the space complexity
here as the I/O operations take place on the disk memory and lesser the consumption of space
lesser will be the I/O operations. That is why the amount by which the disk I/O operations will
be reduced is equal to log(M ). So if there were M I/O operations were to take place, now only
M − log(M ) I/O operations would take place.
92
Exercise 5.2
2M
3− ∗ (B(R) + B(S))(3 − 0.2) ∗ 20000 = 56000
B(S)
Exercise 5.3
Exercise 5.4
No matter how many members have, each group, requires only one tuple in main memory. Thus,
we do not need modifications, in fact, memory use will be pleasantly small.
Exercise 5.5
a)
Initially read and output all the tuples in relation R. Then, for every tuple t in S use the index
for attribute R.a to match the tuples on the relation of R with t.a. If t is not present on those
tuples, then output the tuple t.
This operation works efficiently if the relation S contains small values and R contains large
values. So that the main memory does not exceed the size.
b)
Initially read and output all the tuples in relation S. Then, for every tuple t in S use the index
for attribute R.a to match the tuples on the relation of R with t.a. If t is not present on those
tuples, then output the tuple t.
This operation works efficiently if the relation S contains small values and R contains large
values. So that the main memory does not exceed the size.
c)
This operation uses the index for R.a to process the blocks of ’R’ relation. With given index key,
organize each block and if there is only one tuple for the given index key the output that tuple t.
Exercise 6.2
a)
10000
k
93
b)
500000
k
c)
We need to retrieve every block of R, which in this case is 10000 disk I/O’s.
Exercise 6.3
a)
B(R)
∗ Key = 1000
V (R, a)
b)
T (R)
∗ Key = 50000
V (R, a)
c)
We need to retrieve every block of R, which in this case is 10000 disk I/O’s.
Exercise 6.4
Exercise 6.5
With the index, we have only to retrieve the blocks containing MovieStar records for the stars
of King Kong. We don’t know how many stars there are, but it is safe to assume that their
records will occupy many more blocks than there are stars in the two King Kong movies. Thus,
using the index allows us to take the join while accessing only a small part of the MovieStar
relation and is therefore to be preferred to a sort- or hash-join, each of which will access the
entire MovieStar relation at least once.
Exercise 6.6
Due to skipping pf sorting phase, it reads one block of S and one block read R and this method
works as long as the number of tuples present in the relation R and S which contains the same
Y -value that fit into M blocks.
94
15.7 Buffer Management
Exercise 7.1
a)
The terms B(R) and B(S) represents binary relations between the relations R and S. In one-
pass, there is an approximate requirement for the binary operation between the relations R and
S as:
min(B(R), B(S)) ≤ M
This approximation means that the one buffer is used to read the blocks of larger relation and for
smaller relations, it requires M buffers additionally with the main memory structure. According
to the one-pass algorithm and given terms, the relation either R or S must fit into the memory.
So, the approximation rule would be:
M
min(B(R), B(S)) ≥
2
b)
This algorithm might work properly only if the one-pass union, intersection and difference be-
tween the relations Ri and Si whose sizes are found to be B(R) B(S)
M −1 and M −1 respectively as it is
known, the one-pass algorithm requires operand and it occupies at most M − 1 blocks.
Therefore the two-pass hash based algorithms requires at least min(B(R), B(S)) ≤ M 2 approx-
imately.
M M
Since the worst case of two-pass algorithm contains 2 blocks. The memory between M and 2
requires approximately
M2
min(B(R), B(S)) ≤
4
c)
M2
max(B(R), B(S)) ≤
4
Exercise 7.2
a)
Following the concept of FIFO the new blocks occupies the buffer by emptying the current
longest block in buffer. When doing the nested loop join operation, the FIFO pointer pointing
the longest block of the buffer and it need not points out the first tuple in the buffer.
So it will not improve the number of disk I/O’s on nested loop join operations.
b)
The clock algorithm contains the handle which places the disk on the available buffer by rotating
the handle in clockwise direction. While doing the nested loop join, approach also doesn’t
95
searches for the first tuple as the handle of the clock is present on the available space of the
buffer.
It will not improve the number of disk I/O’s on nested loop join operations.
Exercise 7.3
We could rewrite the σC (R ∩ S) to σC (R) ∩ σC (S). whenever there exists indexes, it would make
the query faster.
Exercise 2.2
a)
Let R(A, B) = {(2, 4)} and S(A, B) = {2, 7}. Then
πA (R ∪ S) = {(2), (2)}
πA (R) ∪ πa (S) = {(2)}
b)
Let R(A, B) = {(2, 4)} and S(A, B) = {2, 7}. Then
πA (R − S) = {(3)}
πA (R) − πA (S) = ∅
πA (R −B S) = {(3)}
πA (R) −B πA (S) = ∅
c)
96
Let R(A, B) = {(2, 4), (2, 5)}
d)
Let R(A, B) = {(2, 4), (2, 4)} and S(A, B) = {2, 4}
δ(R ∪B S) = {2, 4}
δ(R) ∪B δ(S) = {(3, 4), (3, 4)}
Exercise 2.3
For every tuple ri of relation R and every tuple sj of relation S, we could have the following
πL (R ∪B S) = πL (ri , sj )
= πL (ri ), πL (sj )
= πL (R) ∪B πL (S)
Exercise 2.4
a)
For every tuple ri of relation R we could have
R ∪ R = {ri }
R ∪B R = {ri , ri }
b)
For every tuple ri of relation R we could have
R ∩ R ∈ {ri }
R ∩B R = {ri , ri }
c)
For every tuple ri of relation R we could have
R−R=∅
R −B R = ∅
97
d)
Let x be the tuples of relation R, S, and T .
R ∪B (S ∩B T ) = R ∪B (x, x)
= x, x, x
(R ∪B S) ∩B (R ∪B T ) = (x, x) ∩B (x, x)
= x, x, x, x
Exercise 2.5
Exercise 2.6
Exercise 2.7
Exercise 2.8
Exercise 2.9
Exercise 2.10
a)
98
b)
πa,R.b,S.b,S.c,T.c,U.d,e (R(a, b) ▷◁R.b=S.b S(b, c)) ▷◁S.c=T.c (T (c, d) ▷◁T.d=U.d U (d, e))
c)
πL (R(a, b) ▷◁R.b=S.b S(b, c)) ▷◁R.a=U.a AND S.c=T.c (T (c, d) ▷◁T.d=U.d U (a, d))
Exercise 3.2
Exercise 3.3
a)
The rule for the given condition is
• Create an expression for subquery S, the project only the empty list of attributes. Apply
δ on the projection to eliminate duplicates.
• Take the product of R and the result from S.
• Check if the product of R and S is empty. If the product is empty, then the projected list
πL is empty. If the product is not empty, the projected list is R.
b)
πL (R ▷◁a=b δ(S))
• Create an expression for the subquery S. If S may have duplicates, apply the δ operator
to S.
• Get the product of R and S based on the condition that equates a to the corresponding
attribute of b of S.
• Project the result of the product to the attributes of L.
c)
πL (R ▷◁a<>b δ(S))
• Create an expression for the subquery S. If S may have duplicates, apply the δ operator
to S.
• Get the product of R and S based on the condition that do not equates a to the corre-
sponding attribute of b of S.
• Project the result of the product to the attributes of L.
99
Exercise 3.4
Exercise 3.5
3! × 3! × 2 = 72
• Flight information is retrieved from database element A and B, which means r1 (A) and
r1 (B).
• Selects a flight and flight reservation is made for the customer, which means r1 (B) and
w1 (B).
• Customer selects a flight seat from database element C, which means r1 (C) and w1 (C).
• Get customer’s credit card number and records flight bill in database element D, which
means r1 (D) and w1 (D).
• Customer’s phone and flight data is inserted to database element E, which means r1 (E)
and w1 (E).
Exercise 1.2
10!
= 210
4! × (10 − 4)!
18.2 Conflict-Serializability
Exercise 2.1
a)
100
I don’t give an example here, because the t and s is different, thus (T1 , T2 ) would be equivalent
to (T2 , T1 ).
b)
Need community help.
c)
According to the definition of serial schedules. The only serial order that we have is (T1 , T2 ) and
(T2 , T1 ). Thus we can
d)
To identify the number of serializable schedule of the 12 given actions, we need to consider the
interleaving of the serial order (T1 , T2 ) and (T2 , T1 ). And we have the following order:
( )2
6!
= 400
3! × (6 − 3)!
Exercise 2.2
a)
We cannot swap the adjacent actions without a conflict, only (T1 , T2 ) is conflict equivalent to
itself. So the answer is 1.
b)
We can swap the adjacent actions without a conflict, so the answer is
c)
( )2
4!
= 36
2! × (4 − 2)!
d)
The number of actions differs. This implies that we have different answers as number of actions
determines the number of possible interleavings for the serializable order.
Exercise 2.3
a)
4!
× 2 = 12
2! × (4 − 2)!
101
b)
( )2
4!
= 36
2! × (4 − 2)!
Exercise 2.4
Exercise 2.5
Exercise 2.6
a)
Suppose that we have the schedule derived from the given transactions.
Note that r1 (A) must hold a lock on A, since a transaction is granted with lock before its read
and write action. Likewise, r2 (A) must also hold a lock on A.
Since the unlock of A for r1 (A) is executed after w1 (A) and r2 (A) appears before w1 (A). There-
fore, the schedule is prohibited.
b)
4! 4!
× + 2 = 38
2! × (4 − 2)! 2! × (4 − 2)!
c)
4! 4!
× = 36
2! × (4 − 2)! 2! × (4 − 2)!
d)
The number of conflict serializable schedule is 2.
e)
All legal schedules are serializable. It is impossible for a legal schedule to be unserializable.
102
Exercise 3.2
Exercise 3.3
Exercise 3.4
103