0% found this document useful (0 votes)
27 views15 pages

Dbms Pue Solution

Uploaded by

kanishk245451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views15 pages

Dbms Pue Solution

Uploaded by

kanishk245451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Roll No.

KIET Group of Institutions

PUE Solution (2024-2025) ODD Semester


Department: CS, CSE(AI), CSE(AI&ML), CSE, CSIT, IT Course: B.Tech.
Year: III Semester: V
Subject Name: Database Management System Subject Code: BCS501

Duration: 3 Hrs. Max. Marks: 100


Note: Attempt all the questions of each section
Section-A (2X10=20)
Competitive
Q. No. Question Exam# CO BL/ KC*
GATE
a. What is the degree (or arity) of a relation in relational database systems? 2023 1 1F/C
Solution:

In relational database systems, the degree (or arity) of a relation refers to the number of attributes (columns) in a relation
(table).

Consider the following Employee relation (table): (Emp_ID, Emp_Name, Emp_Department)


Attributes (columns): Emp_ID, Emp_Name, Emp_Department
Degree = 3 (since there are 3 attributes)

What is the difference between logical data independence and physical data independence?
b. 1 2F/C
Which one is harder to achieve? Why?
Solution:
 Logical data independence is the capacity to change the conceptual schema without having to change external schemas
or application programs. Ex: Adding/removing an attribute, splitting/merging relations, modifying relationships.
Physical data independence is the capacity to change the internal schema without having to change the conceptual
schema. Ex: Changing file organization (e.g., heap to hash storage), adding indexes etc.

 Logical Data Independence is harder to achieve because changes to the logical schema may require changes to multiple
user views and application queries. Physical Data Independence is easier to achieve because changes to physical storage
(like file organization or storage devices) do not affect the logical design or user views.

1 Consider the following three relations in a relational database.


GATE
Employee (eId, Name), Brand(bId, bName), Own(eId, bId)
c. CSE 2 3F/C
Write the relational algebra expression which return the set of eIds who own all the 2022
brands?
Solution:
π eId, bId (Own) ÷ π bId (Brand)

Let X(A,B,C,D) and Y(P,Q,R,S) be two relations in which A is the foreign key of X that
d. refers to the primary key of Y. Which operation causes referential integrity constraint 2 3F/C
violation?
Solution:
X(A, B, C, D) : A is a foreign key that references the primary key of relation Y.
Y(P, Q, R, S) : Let, P is the primary key of relation Y.
Violation cases:
Insertion in detail table: If a new row is inserted into X (detail table) with a value for A that does not exist in value of P in Y
(Master table) then it violates referential integrity.
Deletion from master table: If a row is deleted from Y (Master table), and value of P is referred by value of A in X, then it
violates the constraint.
Update in detail table: If A is updated to a value that does not exist in Y(P), it violates referential integrity.
Update in master table: If Y(P) (primary key) is updated, all foreign key references in X must be updated to maintain
consistency. If this is not done, the constraint is violated.

GATE
e. Consider a relation R(A, B, C, D, E) with the following three functional dependencies. CSE 3 3F/C
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
AB →C ; BC→ D ; C→E; 2022
What are the candidate keys in the relation R?
Solution:
{A, B} + = (A, B, C, D, E)
Candidate Key: (A, B) . It determines all the attributes of the relation R.

f. Why do we need normalization? 3 2F/C


Solution:
Normalization of data can be considered a process of analyzing the given relation schemas based on their FDs and primary
keys to achieve the desirable properties of
(1) minimizing redundancy and
(2) minimizing the insertion, deletion, and update anomalies
It can be considered as a “filtering” or “purification” process to make the design have successively better quality.

Consider the following log sequence of two transactions on a bank account, with initial
balance 12000, that transfer 2000 to a mortgage payment and then apply a 5% interest.
1. T1 start
2. T1 B old=12000 new=10000
3. T1 M old=0 new=2000 GATE
g. 4. T1 commit CSE 4 3F/C
5. T2 start 2006
6. T2 B old=10000 new=10500
7. T2 commit
Now Suppose the database system crashes just before log record 7 is written. When the
system is restarted, what will be the recovery action?
Solution:
1. T1 start
2. T1 B old=12000 new=10000
3. T1 M old=0 new=2000
4. T1 commit
5. T2 start
6. T2 B old=10000 new=10500 <System FAILS>
7. T2 commit
<T1, start> Found and <T1, Commit> Found →REDO(T1)
<T2, start> Found and <T2, Commit> Not found →UNDO(T2)

Provide the examples for horizontal fragmentation and vertical fragmentation in distributed
h. 4 2F/C
database.
Solution:
Horizontal fragmentation divides the table row-wise. Each fragment is a subset of rows (tuples) of the original table.
Example: Account-schema = (account-number, branch-name, balance)
If the banking system has only two branches Hillside and Valleyview then there are two different fragments:
account1 = σbranch-name = “Hillside” (account)
account2 = σbranch-name = “Valleyview” (account)
Vertical fragmentation divides the table column-wise. Vertical fragmentation of r(R) involves the definition of several
subsets of attributes R1, R2, . . . , Rn of the schema R so that R = R1 ∪ R2 ∪ · · · ∪ Rn
Each fragment ri of r is defined by

The fragmentation should be done in such a way that we can reconstruct relation r from the fragments by taking the natural
join

Example:
employee-info-schema = (employee-id, name, designation, and salary) This relation may be fragmented into two different
fragments:
employee-private-info= (employee-id, salary)
employee-public-info= (employee-id, name, and designation)

i. What benefit does rigorous two-phase locking provide? 5 2F/C


Solution:
Rigorous two-phase locking protocol, which requires that all locks be held until the transaction commits. With rigorous two-
phase locking, transactions can be serialized in the order in which they commit. Most database systems implement either
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
strict or rigorous two-phase locking.
j. How Thomas write rule modify the timestamp ordering protocol? 5 2F/C
Solution:
The Thomas’ write rule modifies the second rule of timestamp ordering protocol. The timestamp-ordering protocol requires that
Ti be rolled back if Ti issues write(Q) and TS(Ti) < W-timestamp(Q).
However, here, in those cases where TS(Ti) ≥ R-timestamp(Q), we ignore the obsolete write.

Section-B (4X5=20)
Competitive
Q. No. Question Exam# CO BL/ KC*
Design a generalization–specialization hierarchy for a motor-vehicle sales company. The
company sells motorcycles, passenger cars, vans, and buses. Justify your placement of
a.
attributes at each level of the hierarchy. Explain why they should not be placed at a higher
or lower level.
Solution:
motor-vehicle Higher Level Entity Set

ISA
2 Lower-Level Entity Sets 1 3F/C
because it inherits the
attributes of motor-vehicle

motorcycles Passenger cars vans buses

OR
Distinguish between the following using ERD:
b. i. Strong and weak entity sets
ii. Disjoint and Overlapping Generalization
i. Strong and weak entity sets
 An entity set may not have sufficient attributes to form a primary key. Such an entity set is termed a weak entity
set.
 An entity set that has a primary key is termed a strong entity set
 Example: consider the entity set payment, which has the three attributes:
 payment-number, payment-date, and payment-amount. Payment numbers are typically sequential numbers, starting
from 1, generated separately for each loan. Thus, although each payment entity is distinct, payments for different
loans may share the same payment number. Thus, this entity set does not have a primary key; it is a weak entity set.
 For a weak entity set to be meaningful, it must be associated with another entity set, called the identifying or owner
entity set

Strong Entity Set Weak Entity Set

● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
ii. Disjoint and Overlapping Generalization

Aspect Disjoint Generalization Overlapping Generalization


An entity belongs to at most one An entity can belong to one or more
Definition
subclass. subclasses.
Exclusivity Mutually exclusive subclasses. Subclasses can overlap.
Constraint
"D" for disjoint. "O" for overlapping.
Representation
Example Employee → Manager, Technician. Person → Student, Employee.
Use Case When subclass roles are distinct. When subclass roles can overlap.

Why are Triggers and Cursors used in PL/SQL? Demonstrate any one with program
a.
code.
Solution:

Trigger:
A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is
executed on a database table. A trigger is triggered automatically when an associated DML statement is
executed.
Syntax of Triggers :

The Syntax for creating a trigger is:


CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- sql statements
END;
3 2 3F/C
A cursor is a temporary work area created in the system memory when a SQL statement is
executed. A cursor contains information on a select statement and the rows of data accessed by
it. This temporary work area is used to store the data retrieved from the database, and manipulate
this data. A cursor can hold more than one row, but can process only one row at a time. The set
of rows the cursor holds is called the active set.

There are two types of cursors in PL/SQL:

Implicit cursors:
These are created by default when DML statements like, INSERT, UPDATE, and DELETE
statements are executed. They are also created when a SELECT statement that returns just one
row is executed.

Explicit cursors:
These must be created when you are executing a SELECT statement that returns more than one
row. Even though the cursor stores multiple records, only one record can be processed at a time,
which is called as current row. When you fetch a row the current row position moves to next
row.
Both implicit and explicit cursors have the same functionality, but they differ in the way they
are accessed.

● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
DECLARE var_rows number(5);
BEGIN
UPDATE employee
SET salary = salary + 1000;
IF SQL%NOTFOUND THEN
dbms_output.put_line('None of the salaries where updated');
ELSIF SQL%FOUND THEN
var_rows := SQL%ROWCOUNT;
dbms_output.put_line('Salaries for ' || var_rows || ' employees are updated');
END IF;
END;
OR
Let there be Schemas R (A,B,C) & S(D,E,F) and relations r(R) & s(S). Give expressions
in SQL equivalent to the following Relational Algebra (RA) expressions:
(a) A (r)
b.
(b) B=17 (r)
(c) r s
(d) A, F (C=D (r  s))
Solution:

(a) SELECT A FROM R;


(b) SELECT * FROM R WHERE B=17;
(c) SELECT * FROM R CROSS JOIN S;
OR SELECT * FROM R,S;
(d) SELECT R.A, S.F FROM R CROSS JOIN S WHERE R.C = S.D;

Consider the relation R(P,Q,S,T,X,Y,Z,W) with the following functional dependencies:


PQ→X; P→YX; Q→Y; Y→ZW GATE
a. Consider the following decomposition scheme CSE
D: R=[(P,Q,S,T); (P,T,X); (Q,Y); (Y,Z,W)] 2021
Find whether D is a lossy decomposition or lossless decomposition.
Solution:
4 D is a lossless decomposition 3 3F/C
OR
Define Fourth Normal Form. Consider a Relational Schema R = (A,B,C,D,E).
Let M be the following set of Multi-Valued Dependencies: -
b.
M = (A BC, BCD, EAD)
Give a loss-less join decomposition of R into Fourth Normal Form. Justify your answer.
Solution:
Since A BC holds, by Complementation Rule A (R-A-BC)  DE
So, by Fagin’s Theorem R1 (A, B, C) and R2 (A, D, E) will be loss-less decomposition of R.
OR
Considering BCD, we can prove that R1 (B, C, D) and R2 (B, A, E) will also be a loss-less decomposition of R.
OR
Similarly, considering EAD, we can prove that R1 (E, A, D) and R2 (E, B, C) will also be a loss-less
decomposition of R.

How immediate or deferred database modification scheme is used for recovery? Explain
a. how the logs are maintained and used for recovery in any one of the mentioned schemes
using suitable example.
Solution:

Immediate Database Modification


 Recovery procedure has two operations instead of one:
5 4 3F/C
undo(Ti) restores the value of all data items updated by Ti to their old values, going backwards
from the last log record for Ti
redo(Ti) sets the value of all data items updated by Ti to the new values, going forward from the
first log record for Ti
 Both operations must be idempotent
That is, even if the operation is executed multiple times the effect is the same as if it is executed
once

● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
Needed since operations may get re-executed during recovery
 When recovering after failure:
Transaction Ti needs to be undone if the log contains the record
<Ti start>, but does not contain the record <Ti commit>.
Transaction Ti needs to be redone if the log contains both the record <Ti start> and the record
<Ti commit>.
 Undo operations are performed first, then redo operations.
 Below we show the log as it appears at three instances of time.

 Recovery actions in each case above are:


(a) undo (T0): B is restored to 2000 and A to 1000.
(b) undo (T1) and redo (T0): C is restored to 700, and then A and B are set to 950 and 2050
respectively.
(c) redo (T0) and redo (T1): A and B are set to 950 and 2050 respectively. Then C is set to 600

Deferred Database Modification


 During recovery after a crash, a transaction needs to be redone if and only if both <Ti start>
and<Ti commit> are there in the log.
 Redoing a transaction Ti ( redoTi) sets the value of all data items updated by the transaction to the
new values.
 Crashes can occur while the transaction is executing the original updates, or while recovery action
is being taken
 example transactions T0 and T1 (T0 executes before T1):

T0: read (A) T1 : read (C)


A: - A - 50 C:- C- 100
Write (A) write (C)
read (B)
B:- B + 50
write (B)
Below we show the log as it appears at three instances of time

 If log on stable storage at time of crash is as in case:


(a) No redo actions need to be taken
(b) redo(T0) must be performed since <T0 commit> is present
(c) redo(T0) must be performed followed by redo(T1) since
<T0 commit> and <T1 commit> are present
OR
What is the importance of maintaining ACID properties in transactions? Also define the
b.
transaction states with diagram.
Solution:
Let Ti be a transaction that transfers $50 from account A to account B. This transaction can be defined
as: Ti: read(A); A := A - 50; write(A); read(B); B := B + 50; write(B).

Let us now consider each of the ACID requirements:

Atomicity: Suppose that, just before the execution of transaction Ti the values of accounts A and B are
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
$1000 and $2000, respectively. Now suppose that, during the execution of transaction Ti, a failure occurs
that prevents Ti from completing its execution successfully.
Further, suppose that the failure happened after the write(A) operation but before the write(B) operation.
In this case, the values of accounts A and B reflected in the database are $950 and $2000. The system
destroyed $50 as a result of this failure. In particular, we note that the sum A + B is no longer preserved.
That is the reason for the atomicity requirement:
If the atomicity property is present, all actions of the transaction are reflected in the database, or none are.

Consistency: The consistency requirement here is that the sum of A and B be unchanged by the execution
of the transaction. Without the consistency requirement, money could be created or destroyed by the
transaction.

Durability: The durability property guarantees that, once a transaction completes successfully, all the
updates that it carried out on the database persist, even if there is a system failure after the transaction
completes execution.

Isolation: Even if the consistency and atomicity properties are ensured for each transaction, if several
transactions are executed concurrently, their operations may interleave in some undesirable way, resulting
in an inconsistent state.

Transaction states with diagram:


• Active, the initial state; the transaction stays in this state while it is executing
• Partially committed, after the final statement has been executed
• Failed, after the discovery that normal execution can no longer proceed
• Aborted, after the transaction has been rolled back and the database has been restored to its
state prior to the start of the transaction
• Committed, after successful completion

State diagram of a transaction

What is the validation-based concurrency-control scheme? Show that by choosing


a. Validation (Ti), rather than Start (Ti), as the timestamp of transaction Ti, we can expect
better response time provided that conflict rates among transactions are indeed low.
Solution:
1. Read phase. During this phase, the system executes transaction Ti. It reads the values of the various
data items and stores them in variables local to Ti. It performs all write operations on temporary local
variables, without updates of the actual database.
2. Validation phase. Transaction Ti performs a validation test to determine whether it can copy to the
database the temporary local variables that hold the results of write operations without causing a violation
6 of serializability.
3. Write phase. If transaction Ti succeeds in validation (step 2), then the system applies the actual updates 5 3F/C
to the database. Otherwise, the system rolls back Ti.

Three different timestamps with transaction Ti:


1. Start(Ti), the time when Ti started its execution.
2. Validation(Ti), the time when Ti finished its read phase and started its validation phase.
3. Finish(Ti), the time when Ti finished its write phase

The reason we have chosen Validation(Ti), rather than Start(Ti), as the timestamp of transaction Ti is that
we can expect faster response time provided that conflict rates among transactions are indeed low.
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
OR
What is Two-Phase-Locking Protocol? Consider the following transactions:-
T1 : Read (A);
Read (B);
If A = 0 then B:= B+1;
Write (B);
b. T2 : Read (B);
Read (A);
If B = 0 then A:= A+1;
Write (A);
Add Lock and Unlock instructions to Transactions T1 and T2, so that they observe the
Two-Phase-Locking Protocol.
Solution:
One protocol that ensures serializability is the two-phase locking protocol. This protocol requires that
each transaction issue lock and unlock requests in two phases:
1. Growing phase. A transaction may obtain locks, but may not release any lock.
2. Shrinking phase. A transaction may release locks, but may not obtain any new locks.

Add Lock and Unlock instructions to Transactions T1 and T2:


T1: Read (A);
Read (B);
If A = 0 then B:= B+1;
Write (B);
T2: Read (B);
Read (A);
If B = 0 then A:= A+1;
Write (A);
Lock and unlock instructions:
T1:
lock-S(A)
read(A)
lock-X(B)
read(B)
if A = 0
then B := B + 1
write(B)
unlock(A)
unlock(B)
T2:
lock-S(B)
read(B)
lock-X(A)
read(A)
if B = 0
then A := A + 1
write(A)
unlock(B)
unlock(A)

Section-C (6X5=30)
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
Competitive
Q. No. Question Exam# CO BL/ KC*
Convert the following ER model to a relational model

a.

Also mention the primary key and foreign key in each relation.
Solution:
student (sid, name, program)
course (courseno, title, syllabus, credits)
7 course-offering (courseno, secno, year, semester, time, room) 1
3F/C
instructor (instructor-id, name, dept, title)
enrols (sid, courseno, secno, semester, year, grade)
teaches (courseno, secno, semester, year, instructor-id)
requires (maincourse, prerequisite)
OR
EasyShop Retail Store: All relations for the retail shop application along with their
attributes are mentioned below. A customer can buy multiple items and will receive
different bills for each item bought. A customer can purchase at more than one retail outlet.
Each retail store will have multiple employees. Employees can work in only one retail
outlet. Managers are also employees. Customers living at the same address are spouses.

Customer (CustId, CustType, CustName, EmailId, ContactNo, Address)


b.
RetailOutlet (ROId, Location, ManagerId)
Item (ItemCode, ItemType, Desc, Price, ReorderLevel, QtyOnHand, Category)
Employee (EmpId, EmpName, Designation, EmailId, ContactNo, WorksIn)
PurchaseBill (BillId, RetailOutlet, Item, Customer, BillAmount, Bill Date, Quantity)

Draw the ER Diagram for the above scenario. Mention the necessary assumptions if you
have made to design this application.
Solution:

EMPLOYEE (EmployeeID, EmployeeName, Street, City)


8 a. COMPANY (CompanyID, Company Name, City) 2 3F/C
WORKS (EmployeeID, CompanyID, Salary)
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
MANAGES (EmployeeID, ManagerID)

Using above schema Write equivalent SQL or Relational Algebra queries for the given
questions.
i. Display the total number of employees working in each company and average
salary is greater than 5000
ii. Find the second highest salary of an employee.
iii. List the employees details who are working in TCS company located in
Bangalore.
iv. Find the names and cities of residence of employees working for TCS
v. Find the names of employees, who are working in the same city where they live.
vi. Find the names of employees, who are not working for WIPRO

Solution:

i. Select Count(*) AS total_emp


from Works Group by CompanyID
Having AVG(Salary)>5000;

ii. Select Max(Salary)


from Works
where Salary < (Select Max(Salary) from Works);

iii. Select * from Employee e, Company c, Works w


Where e.employeeID = w.employeeID AND w.CompanyID = c.CompanyID AND
CompanyName =’TCS’ AND c.City =’ Banglore’;

iv. SELECT e.EmployeeName, e.City AS CityOfResidence


FROM EMPLOYEE e, WORKS w, COMPANY c
Where e.EmployeeID = w.EmployeeID AND w.CompanyID = c.CompanyID AND
c.CompanyName = 'TCS';

v. SELECT E. EmployeeName
FROM EMPLOYEE E JOIN WORKS W ON E.EmployeeID = W.EmployeeID JOIN
COMPANY C ON W.CompanyID = C.CompanyID
WHERE E.City = C.City;

vi. SELECT DISTINCT E. EmployeeName


FROM EMPLOYEE E JOIN WORKS W ON E. EmployeeID = W.EmployeeID JOIN
COMPANY C ON W.CompanyID = C.CompanyID
WHERE C.CompanyName != 'WIPRO';

OR
Consider the following schema for institute library:
Student (RollNo, Name, Father_ Name, Branch)
Book (ISBN, Title, Author, Publisher)
Issue (RollNo, ISBN, Date_of_Issue)
b.
Write the following queries in SQL and relational algebra:
i. Find the name of student who has issued a book published by 'ABC' publisher.
ii. List title of all books and their authors issued to a student 'RAM'.
iii. List title of all books issued on or before December 1, 2020.

Solution:
i. SELECT DISTINCT S.Name
FROM Student S
JOIN Issue I ON S.RollNo = I.RollNo
JOIN Book B ON I.ISBN = B.ISBN
WHERE B.Publisher = 'ABC';

Type equation here.


ii. SELECT B.Title, B.Author
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
FROM Student S
JOIN Issue I ON S.RollNo = I.RollNo
JOIN Book B ON I.ISBN = B.ISBN
WHERE S.Name = 'RAM';

iii. SELECT DISTINCT B.Title


FROM Book B
JOIN Issue I ON B.ISBN = I.ISBN
WHERE I.Date_of_Issue <= '2020-12-01';

A set of FDs for the relation R{A, B, C, D, E, F} is AB →C, C → A, BC →


a. D, ACD → B, BE → C, EC → FA, CF → BD, D→ E.
Find a minimum cover for the set of FDs
Solution:
Step 1: Each FD has single attribute in RHS
 AB→C
 C→A
 BC→D
 ACD→B
 BE→C
 EC→F
 EC→A
 CF→B
 CF→D
 D→E

Step 2: No FD has extraneous attribute. Remove extraneous attributes from the left-hand side.

For AB→C: We check if A→C or B→C can be derived from the rest of the FDs. We find that C→A and
BE→C do not provide direct evidence that A or B alone can determine C. So, no extraneous attributes
are removed.

For BC→D: Check if B→D or C→D can be derived. Since no direct derivation exists, no extraneous
attributes are removed.
9 For ACD→B: We check if AC→B, AD→B, or CD→B can be derived. We see that ACD→B is not 3 3F/C
redundant and no attributes can be removed.

For BE→C: Check if B→C or E→C can be derived. No direct derivation exists, so no attributes are
removed.
EC→F: Check if E→F or C→F can be derived, No direct derivation exists, so no attributes are removed.

EC→A: Check if E→A or C→A can be derived, No direct derivation exists, so no attributes are removed
For CF→B: Check if C→B or F→B can be derived, No direct derivation exists, so no attributes are
removed
For CF→D: Check if C→D or F→D can be derived, No direct derivation exists, so no attributes are
removed

For D→E: This FD cannot be reduced further, as no attributes are extraneous.

Step 3: No extraneous FD in set: Remove redundant FDs


ACD→B is redundant: Removing ACD→B still leaves us with the necessary functional dependencies,
so it is redundant and can be removed.
So final Minimal Cover set: Fc = {AB→C, C→A, BC→D, BE→C, EC→F, EC→A, CF→B, CF→D
D→E}
OR

For R (A, B, C, D) with the following FDs, (i) Determine its Candidate Keys (ii) What best
NF is it in? and (iii) Decompose R into BCNF Schema, If it is not in BCNF.
b. (a) C→D, C→A, B→C
(b) B→C, D→A
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
Solution:
(a) CD, CA, BC

{B}+ =(B,C,D,A)
Candidate Key: {B}
There is a transitive dependency BC, CDA, but no partial FD. So, R is in 2 NF but not in
3NF.
BCNF Decomposition of R
R1 (C, D, A) CDA PK (C)
R2 (B, C) BC PK (B)
FK(C) references R1

(b) BC, DA


{BD}+ = (B, D, C, A)
Candidate Key: {B,D}
BC, DA are partial FDs. So, R is in 1 NF.
BCNF Decomposition of R
R1 (B, C )
PK (B)
BC
R2 (D, A)
PK (D)
DA
R3 (B, D)
PK (B, D)

FK (B) references R1, FK (D) references R2


Consider the following read-write schedule S over three transactions T1, T2 and T3 where the
subscripts in the schedule indicate transaction IDs: GATE CSE
2024
S1: r1(z); w1(z); r2(x); r3(y); w3(y); r2(y); w2(x); w2(y); GATE CSE
a.
S2: r1(x); r2(x); r2(y); w2(y); r1(y); w1(x); 2021
What is the conflict serializable schedule? Which of the above schedules is conflict serializable?
For each serializable schedule, determine the equivalent serial schedule.
Solution:

S1: r1(z); w1(z); r2(x); r3(y); w3(y); r2(y); w2(x); w2(y);


T1 : No edge
T2 : No outgoing Edge
T3 → T2

There is no cycle. Therefore, it is a conflict serializable schedule


Equivalent Serial schedule: <T1, T3, T2> OR <T3, T1, T2> OR <T3, T2, T1>

S2: r1(x); r2(x); r2(y); w2(y); r1(y); w1(x);


10 4
3F/C
Only Edge: T2 → T1
There is no cycle. Therefore, it is a conflict serializable schedule
Equivalent Serial schedule: <T2, T1>

Conflict serializable schedule:

Let us consider a schedule S in which there are two consecutive instructions Ii and Ij, of transactions Ti
and Tj, respectively (i ∕= j). If Ii and Ij refer to different data items, then we can swap Ii and Ij without
affecting the results of any instruction in the schedule. However, if Ii and Ij refer to the same data item
Q, then the order of the two steps may matter. Since we are dealing with only read and write
instructions, there are four cases that we need to consider:
1. Ii = read(Q), Ij = read(Q). The order of Ii and Ij does not matter, since the same value of Q is
read by Ti and Tj, regardless of the order.
2. Ii = read(Q), Ij = write(Q). If Ii comes before Ij, then Ti does not read the value of Q that is
written by Tj in instruction Ij. If Ij comes before Ii, then Ti reads the value of Q that is written
by Tj. Thus, the order of Ii and Ij matters.
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
3. Ii = write(Q), Ij = read(Q). The order of Ii and Ij matters for reasons similar to those of the
previous case.
4. Ii = write(Q), Ij = write(Q). Since both instructions are write operations, the order of these
instructions does not affect either Ti or Tj. However, the value obtained by the next read(Q)
instruction of S is affected, since the result of only the latter of the two write instructions is
preserved in the database. If there is no other write(Q) instruction after Ii and Ij in S, then the
order of Ii and Ij directly affects the final value of Q in the database state that results from
schedule S.
OR
What is deadlock? When does it occur? How can it be avoided, detected and recovered in
b. database system? Under what condition is it less expensive to avoid deadlock than to allow
deadlocks to occur and then to detect them.
Solution:

Deadlock:
A system is in a deadlock state if there exists a set of transactions such that every transaction in the set is
waiting for another transaction in the set. More precisely, there exists a set of waiting transactions {T0,
T1, . . ., Tn} such that T0 is waiting for a data item that T1 holds, and T1 is waiting for a data item that
T2 holds, and . . ., and Tn−1 is waiting for a data item that Tn holds, and Tn is waiting for a data item
that T0 holds. None of the transactions can make progress in such a situation.

Deadlocks occur when the following four conditions hold simultaneously (referred to as the Coffman
conditions):
1. Mutual Exclusion: At least one resource is held in a non-sharable mode.
2. Hold and Wait: Transactions holding some resources are waiting for additional resources held
by others.
3. No Preemption: Resources cannot be forcibly taken away from a transaction.
4. Circular Wait: A set of transactions are waiting on each other in a circular chain.
Deadlock Handling in Database Systems
1. Deadlock Avoidance
Avoiding deadlocks involves preventing the circular wait condition or ensuring the system does not enter
an unsafe state:
 Wait-Die Scheme: Older transactions can wait for younger ones to release resources, but
younger transactions are aborted if they request resources held by older ones.
 Wound-Wait Scheme: Older transactions preempt resources from younger ones (forcing
younger ones to abort), but younger transactions must wait for older ones.
 Resource Ordering: Impose a global order on resource allocation to prevent circular waits.
2. Deadlock Detection
Allows deadlocks to occur and then detects and resolves them:
 Wait-for Graph (WFG):
o Nodes represent transactions.
o Edges represent "waiting for" relationships.
o A cycle in the graph indicates a deadlock.
Detection algorithms periodically scan for cycles in the WFG to identify deadlocks.
3. Deadlock Recovery
Once a deadlock is detected, the system resolves it by aborting one or more transactions:
 Transaction Rollback: Abort one or more transactions involved in the deadlock.
 Transaction Selection: Choose a victim transaction to abort based on criteria like:
o Least progress made (minimizes wasted work).
o Lowest priority.
o Least resource utilization.

Deadlock avoidance is less expensive when:


 High Frequency of Deadlocks: Frequent deadlocks require frequent detection and recovery,
making avoidance preferable to reduce overhead.
 High Cost of Rollback: If aborting transactions involves significant work loss (e.g., long-
running transactions or transactions with complex operations), avoidance is cheaper.
 Low Concurrency Requirements: In systems with limited concurrency, avoidance techniques
(like resource ordering) introduce negligible overhead.
 Real-Time Constraints: In real-time systems where transaction delays are unacceptable,
deadlock avoidance ensures smooth processing without interruptions.
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
What is multiple granularity concurrency control scheme? How the concurrency is
maintained in this case. Write the concurrent transaction for the following graph:

a.

 T1 wants to access the item C in read mode


 T2 wants to access the item D in exclusive mode
 T3 wants to read all the children of item B
 T4 wants to access all item in read mode
Solution:
Multiple granularity concurrency control scheme:

The Multiple Granularity Concurrency Control Scheme is a mechanism that allows transactions to lock
data at different levels of granularity (e.g., database, table, page, row) to improve concurrency and
minimize locking overhead. By locking only the required level of granularity, the system optimizes
resource usage and allows finer control over access to data.

11 5 2F/C
Compatibility Matrix

There is an intention mode associated with shared mode, and there is one with exclusive mode.
If a node is locked in intention-shared (IS) mode, explicit locking is being done at a lower level of the
tree, but with only shared-mode locks.
Similarly, if a node is locked in intention-exclusive (IX) mode, then explicit locking is being done at a
lower level, with exclusive-mode or shared-mode locks.
Finally, if a node is locked in shared and intention-exclusive (SIX) mode, the subtree rooted by that node
is locked explicitly in shared mode, and that explicit locking is being done at a lower level with exclusive-
mode locks. The compatibility function for these lock modes is in above Figure.

The multiple-granularity locking protocol, which ensures serializability, is this: Each transaction Ti can
lock a node Q by following these rules:
1. It must observe the lock-compatibility function of Figure 16.17.
2. It must lock the root of the tree first, and can lock it in any mode.
3. It can lock a node Q in S or IS mode only if it currently has the parent of Q
locked in either IX or IS mode.
4. It can lock a node Q in X, SIX, or IX mode only if it currently has the parent of
Q locked in either IX or SIX mode.
5. It can lock a node only if it has not previously unlocked any node (that is, Ti is two phase).
6. It can unlock a node Q only if it currently has none of the children of Q locked

Transactions and Lock Requirements:

T1 (Read C):
Acquires IS lock on A → IS lock on B → S lock on C.
T2 (Exclusive on D):
Acquires IX lock on A → IX lock on B → X lock on D.
T3 (Read all children of B):
Acquires IS lock on A → S lock on B → S lock on C, S lock on D.
● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)
T4 (Read all items):
Acquires IS lock on A → S lock on A → S lock on B → S locks on C and D.

To maintain concurrency:

T1 and T3 can start immediately as their locks are compatible.


T4 locks the root node (A) in shared mode, which blocks T2.
Once T3 and T4 release locks on D, T2 can acquire its exclusive lock on D and proceed.

This ensures proper locking hierarchy and avoids deadlocks.


OR
b. Discuss time stamp-based protocol with suitable example.

Solution:

Timestamp:
With each transaction Ti in the system, we associate a unique fixed timestamp, denoted by TS(Ti). This
timestamp is assigned by the database system before the transaction Ti starts execution.

If a transaction Ti has been assigned timestamp TS(Ti), and a new transaction Tj enters the system, then
TS(Ti) < TS(Tj).

There are two simple methods for implementing this scheme:

1. Use the value of the system clock as the timestamp; that is, a transaction’s timestamp is
equal to the value of the clock when the transaction enters the system.
2. Use a logical counter that is incremented after a new timestamp has been
assigned; that is, a transaction’s timestamp is equal to the value of the counter
when the transaction enters the system.
The timestamps of the transactions determine the serializability order. Thus, if TS(Ti) <
TS(Tj), then the system must ensure that the produced schedule is equivalent to a serial
schedule in which transaction Ti appears before transaction Tj.

To implement this scheme, we associate with each data item Q two timestamp values:
• W-timestamp(Q) denotes the largest timestamp of any transaction that executed write(Q)
successfully.
• R-timestamp(Q) denotes the largest timestamp of any transaction that executed read(Q)
successfully.
These timestamps are updated whenever a new read(Q) or write(Q) instruction is executed.

The Timestamp-Ordering Protocol:

1. Suppose that transaction Ti issues read(Q).


a. If TS(Ti) < W-timestamp(Q), then Ti needs to read a value of Q that was already
overwritten. Hence, the read operation is rejected, and Ti is rolled back.

b. If TS(Ti) ≥ W-timestamp(Q), then the read operation is executed, and Rtimestamp(Q) is set
to the maximum of R-timestamp(Q) and TS(Ti).

2. Suppose that transaction Ti issues write(Q).


a. If TS(Ti) < R-timestamp(Q), then the value of Q that Ti is producing was needed
previously, and the system assumed that that value would never be produced. Hence, the
system rejects the write operation and rolls Ti back.
b. If TS(Ti) < W-timestamp(Q), then Ti is attempting to write an obsolete value of Q. Hence,
the system rejects this write operation and rolls Ti back.
c. Otherwise, the system executes the write operation and sets W-timestamp(Q) to TS(Ti).

● CO -Course Outcome generally refer to traits, knowledge, skill set that a student attains after completing the course successfully.
● Bloom’s Level (BL) - Bloom’s taxonomy framework is planning and designing of assessment of student’s learning.
● *Knowledge Categories (KCs): F-Factual, C-Conceptual, P-Procedural, M-Metacognitive
● #Reference to Competitive Exams (GATE, GPAT, CAT, GRE, TOFEL, NET, etc.)

You might also like