DBMS practice assignment with solutions
DBMS practice assignment with solutions
t1 t2
R(x)
W(x)
R(x)
W(x)
R(y)
W(y)
R(y)
W(y)
W(x)
R(y)
W(y)
R(x)
W(x)
R(y)
W(y)
As transaction T1 performed the first operation we wrote all of its operations on data item X
as well as Y first and then all the operations of T2 are written next.
Two schedules are said to be viewed as equivalent if they satisfy the following three
conditions:
In the example above, the Initial read is made by the T1 transaction for both the data items X
and Y in Schedule S1 and in S2 as well as initial read is made by transaction T1 for both.
Thus, the initial read condition is satisfied in S1 and S2 for data items X and Y.
The final write for data items X and Y are made by Transaction T2 in Schedule S1. Also, the
final write is made by T2 in Schedule S2 in Schedule S1 as well as serial schedule S2. Thus,
the condition for the final write is also satisfied.
As all three conditions are satisfied for our schedule S1 and its Serial Schedule S2, they are
view equivalent. Thus, we can say that the given schedule S1 is the view-serializable
schedule and is consistent.
(CO2) a) Write an SQL query to create a “Students” table with the following attributes,
specifying appropriate data types : StudentID, Name, Marks. StudentID is a primary
key, Name should not be null and Marks should be greater than 0.
b) Write an SQL query to fetch the name of students whose marks is greater than
50.
Answer
a)
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Marks INT CHECK (Marks > 0));
b)
SELECT Name
FROM Students
WHERE Marks > 50;
(CO4) What are various modes of locks? Explain two-phase locking protocols.
S: R1(X); R2(Z); R1(Z); R3(X); R3(Y); W1(X); W3(Y); R2(Y); W2(Z); W2(Y)
R3(X) → W1(X)
→ T3 reads X, T1 writes X
→ T3 → T1
W1(X) conflicts with R3(X)
R3(Y) → W3(Y)
→ T3 reads Y, then writes Y
W3(Y) → R2(Y)
→ T3 writes Y, then T2 reads Y
→ T3 → T2
W3(Y) → W2(Y)
→ T3 writes Y, then T2 writes Y
→ T3 → T2
Nodes: T1, T2, T3
Edges:
T3 → T1
T3 → T2
The graph:
T3 → T1
T3 → T2
Since T3 must come before both T1 and T2, one possible conflict-equivalent serial
schedule is: T3 → T1 → T2
SELECT name
FROM student JOIN enrolledIn ON s.id = e.id
WHERE code = 'cs3020';
b) Names of students in cs1500 or cs3010
SELECT name
FROM student
JOIN enrolledIn ON s.id = e.id
WHERE code IN ('cs1500', 'cs3010');
c) Who teaches cs1500 or cs3020:
SELECT lecturer
FROM subject
WHERE code IN ('cs1500', 'cs3020');