You are on page 1of 4

CS370 - Introduction to Database Systems

End-Sem Solutions
1. TODO: ER Schema & Relational Schema
2. Relational algebra
(a)

fname,
minit,
lname,
address

smith ssn=superssn

smith ssn

ssn

fname=John
lname=Smith
employee

sex=female
employee
(i)
pname,
plocation

dnumber=dnum

dnumber

ssn=mgrssn

fname=George
lname=Mathew
employee
department
project
(ii)
(b) The expression
_

empid
(emp)
empid
_
emp
salary>sal
_

eid,sal
(emp)
_
__
returns employees with
lowest salary, when emp(empid, salary) is a non-empty relation.
3. Tuple relational calculus expressions
(a)
_
e.ssn, e.fname, e.minit, e.lname

employee(e)
p w
_
project(p) works on(w)
e.ssn = w.essn w.pno = p.pnumber p.pname = superJet
x
_
_
works on(x) x.pno = p.pnumber
_

_
x.hours w.hours
_
_
__
(b)
_
e.ssn, e.fname, e.minit, e.lname

employee(e)
d
_
_
dependent(d) (d.essn = e.ssn)


_
d.sex = female

__
4. SQL queries
(a) select x.pno
from (select w.pno
from works_on as w, employee as e
where e.fname = John and e.lname = Smith
and w.essn = e.ssn
) as x,
(select w.pno
from works_on as w, employee as e
where e.fname = Susan and e.lname = Smith
and w.essn = e.ssn
) as y
where x.pno = y.pno
;
1
(b) select w.pno, count(*) as fcount
from works_on as w, employee as e
where e.ssn = w.essn
and e.sex = female
group by w.pno
having count(*) >= 4
;
(c) select e.dno, count(*) as ecount, sum(dcount) as dcount
from employee as e,
(select d.essn, count(*) as dcount
from dependent as d
group by d.essn
) as x
where e.ssn = x.essn
group by e.dno
;
5. TODO: Disk access for nested-loop join & B
+
-tree index structure.
(a) TODO:
(b) TODO:
6. Functional dependency: State True or False.
(a) (True) A relation in which every key is a singleton is always in 2NF.
(b) (False) A relation scheme in 3NF cannot have any transitive dependencies.
(c) (False) Given a relation R and a set of FDs on it, a lossless, dependency-preserving decompo-
sition of R into BCNF relations can always be determined.
(d) (True) Any relation that has exactly two attributes is in BCNF.
(e) (True) Every functional dependency is also a multi-valued dependency.
(f) (True) Given the set of all FDs on a relation scheme, one can algorithmically determine the
keys of R.
7. Transaction processing
Some basics: Two operations in a schedule are said to be in conict if they belong to dierent
transactions and access same data and at least one of them is a write operation. So, by denition, 2
read operations can never be in conict. Three types of conict are recognized in schedules (a) Read
After Write (RAW); (b) Write After Read (WAR); and (c) Write After Write (WAW). A RAW
conict means a transaction writes some data that is subsequently read by another transaction i.e.
Read happens after Write. The WAR & WAW conicts have similar interpretation.
A Precedence Graph or Serialization Graph, of a schedule, is a directed graph where vertices represent
transactions and edges represent precedence relation between transactions. An edge is drawn from
T
1
to T
2
if an operation in T
1
precedes and conicts with some operation in T
2
. Self loops are absent
because conicts do not arise between operations in the same transaction.
A schedule is serializable if and only if the corresponding precedence graph is acyclic. A schedule S
is conict-serializable if it has a serial schedule S

containing the same set of conicts in S.


(a) Draw Serialization graph, determine if schedules are conict-serializable and give equivalent
serial schedule for the following schedules
S1: r
3
(X), r
2
(X), w
2
(X), r
1
(X), w
1
(X), w
3
(X), r
2
(Y ), w
2
(Y ), r
1
(Y ), w
1
(Y );
S2: r
3
(X), w
3
(X), r
2
(X), w
2
(X), r
1
(X), w
1
(X), r
2
(Y ), w
2
(Y ), r
1
(Y ), w
1
(Y );
2
Approach: (a) Identify conicts in schedule, (b) draw precedence graph, (c) look for cycles,
and (d) produce the serial schedules if precedence graph is acyclic.
Solution for S1: Figure-1 in page 3 shows the conicts in S1 and the corresponding precedence
graph. This graph has 3 basic cycles {T1 T3 T1; T2 T3 T2; T1 T3 T2 T1}.
Therefore schedule S1 is not serializable, so it does not have an equivalent conict-serializable
schedule.
Time T1 T2 T3 RAW WAR WAW
1 r
3
(X)
2 r
2
(X)
3 w
2
(X) r
3
(X)
4 r
1
(X) w
2
(X)
5 w
1
(X)
r
3
(X),
r
2
(X)
w
2
(X)
6 w
3
(X)
r
2
(X),
r
1
(X)
w
2
(X),
w
1
(X)
7 r
2
(Y )
8 w
2
(Y )
9 r
1
(Y ) w
2
(Y )
10 w
1
(Y ) r
2
(Y ) w
2
(Y )
(i)
T1
T2
T3
X
{X, Y }
X
X
X
(ii)
Figure 1: (i) Conicts in schedule S1 and (ii) the corresponding precedence graph
Solution for S2: Figure-2 in page 3 shows the conicts in S2 and the corresponding precedence
graph. This graph has no cycles and the equivalent conict-serializable schedule is {T3 T2
T1}.
Time T1 T2 T3 RAW WAR WAW
1 r
3
(X)
2 w
3
(X)
3 r
2
(X) w
3
(X)
4 w
2
(X) r
3
(X) w
3
(X)
5 r
1
(X)
w
3
(X),
w
2
(X)
6 w
1
(X)
r
3
(X),
r
2
(X)
w
3
(X),
w
2
(X)
7 r
2
(Y )
8 w
2
(Y )
9 r
1
(Y ) w
2
(Y )
10 w
1
(Y ) r
2
(Y ) w
2
(Y )
(i)
T1
T2
T3
{X, Y }
X
X
(ii)
Figure 2: (i) Conicts in schedule S2 and (ii) the corresponding precedence graph.
(b) Dene unrecoverable schedule.
A schedule is said to be unrecoverable if it is not a recoverable schedule. A schedule S is said
to be recoverable if and only if it satises the following property.
Whenever a transaction T
1
writes some data that is read by some transaction T
2
then
T
2
commits only after T
1
commits its changes.
The transaction T
1
is a producer of data and T
2
is a consumer of this data. When the producer
commits rst, the data read by the consumer becomes valid and data inconsistency due to
operation interleaving in the schedule is eliminated.
3
8. Explain Steal/No-Steal, Force/No-Force.
In a DBMS, changes (updates, inserts, deletes) made by a transaction is rst applied in cache/memory
(organized as pages, buers and buer pools) and latter written to disk (permanent storage). The
dierent modes of writing changes to disk are
Steal: write an updated buer to disk before the transaction commits.
No-Steal: do not write an updated buer to disk before the transaction commits.
Force: write the updated buer (all changes) to disk, immediately, when the transaction commits.
No-Force: updated buer is not written immediately when transaction commits, but some time
latter.
End of Solution
4

You might also like