Dbms Pue Solution
Dbms Pue Solution
In relational database systems, the degree (or arity) of a relation refers to the number of attributes (columns) in a relation
(table).
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.
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.
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)
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
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
● 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
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 :
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:
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:
● 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.
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.
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.
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.
Draw the ER Diagram for the above scenario. Mention the necessary assumptions if you
have made to design this application.
Solution:
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:
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;
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';
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 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) CD, CA, BC
{B}+ =(B,C,D,A)
Candidate Key: {B}
There is a transitive dependency BC, CDA, but no partial FD. So, R is in 2 NF but not in
3NF.
BCNF Decomposition of R
R1 (C, D, A) CDA PK (C)
R2 (B, C) BC PK (B)
FK(C) references R1
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.
a.
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
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:
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).
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.
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).
● 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.)