20it403 DBMS Digital Material Unit Iii
20it403 DBMS Digital Material Unit Iii
20it403 DBMS Digital Material Unit Iii
This document is confidential and intended solely for the educational purpose of
RMK Group of Educational Institutions. If you have received this document
through email in error, please notify the system manager. This document
contains proprietary information and is intended only to the respective group /
learning community as intended. If you are not the addressee you should not
disseminate, distribute or copy through e-mail. Please notify the sender
immediately by e-mail if you have received this document by mistake and delete
this document from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in reliance on
the contents of this information is strictly prohibited.
20IT403
DATABASE MANAGEMENT SYSTEMS
Department: IT
Batch/Year:2020-2024/II
Created by:
R.Asha
Date:23/04/2022
1.TABLE OF CONTENTS
1. Contents
2. Course Objectives
3. Pre Requisites
4. Syllabus
5. Course outcomes
7. Lecture Plan
9. Lecture Notes
10. Assignments
MANAGEMENT SYSTEMS
CS8492-DATABASE
CS8391-DATA
STRUCTURES
CS8251-
PROGRAMMING IN C
CS8392 – Object
Oriented Programming
2. COURSE OBJECTIVES
To learn how to efficiently design and implement various database objects and
entities
6
3. PRE REQUISITES
7
4. SYLLABUS
DATABASE MANAGEMENT SYSTEMS
Query Processing Overview – Algorithms for SELECT and JOIN operations – Query
optimization using Heuristics and Cost Estimation
CO6: Design and deploy an efficient and scalable data storage node for varied kind of
application requirements.
6. CO- PO/PSO MAPPING
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 2 1 1 1 1 1 1 2 2 2 2 2
CO2 3 2 2 1 1 1 1 2 2 2 2 2
CO3 2 1 1 1 1 1 1 2 2 2 2 2
CO4 2 1 1 1 1 1 1 2 2 2 2 2
CO5 2 1 1 1 1 1 1 2 2 2 2 2
CO6 2 1 1 1 1 1 1 2 2 2 2 2
Crossword Puzzle
Transactions
8. Activity Based Learning
Brainstorming Session
The term transaction refers to a collection of operations that form a single logical unit of
work.
For Example, transfer of money from one account to another is a transaction consisting of
two updates, one to each account.
A transaction is delimited by statements (or function calls) of the form begin transaction
and end transaction.
The transaction consists of all operations executed between the begin transaction and end
transaction.
Let Ti be a transaction that transfers $50 from account A to account B. This transaction can
be defined as:
T i: read(A);
A := A−50;
write(A);
read(B);
B := B + 50;
write(B).
9.1 Transaction Concepts - ACID Properties
ACID Properties
Atomicity
“Either all operations of the transaction are reflected properly in the
database, or none are.”
Assume that, before the execution of transaction Ti, the values of accounts A and
B are $1000 and $2000, respectively.
Now suppose that, during the execution of transaction Ti, a 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.
The sum of A+B before and after the execution of transaction is not same and the
database is now in inconsistent state.
We must ensure that such inconsistencies are not visible in a database system.
The database system keeps track (on disk) of the old values of any data on which
a transaction performs a write.
If the transaction does not complete its execution, the database system restores
the old values from the log to make it appear as though the transaction never
executed.
Consistency:
“The consistency requirement here is that the sum of A and B be
unchanged by the execution of the transaction.”
Isolation:
“If several transactions are executed concurrently, their operations may
For example:
Durability:
We assume for now that a failure of the computer system may result in loss of
data in main memory, but data written to disk are never lost.
1. The transaction updates have been written to disk before the transaction
completes.
2. Information about the transaction updates and the data written to disk is
sufficient to enable the database to reconstruct the updates when the database
system is restarted after the failure.
Information residing in volatile storage does not usually survive system crashes.
Nonvolatile storage:
Secondary storage devices such as magnetic disk and flash storage, used for
online storage.
Tertiary storage devices such as optical media, and magnetic tapes, used for
archival storage.
Stable storage:
Updates must be done with care to ensure that a failure during an update to
stable storage does not cause a loss of information.
Clearly, the degree to which a system ensures durability and atomicity depends on
how stable its implementation of stable storage really is.
9.1. Transaction Concepts – ACID Properties
Once the changes caused by an aborted transaction have been undone, we say that
the transaction has been rolled back. It is part of the responsibility of the recovery
scheme to manage transaction aborts. This is done typically by maintaining a log.
States of a Transaction:
Active - the initial state; the transaction stays in this state while it is executing.
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.
Active State:
When a transaction finishes its last statement, it enters the partially committed
state.
Committed state:
Once the transaction is committed, the updates of the transaction are made
permanent to the database.
Failed state:
If the consistency check fails, the transaction is aborted and rolled back.
The transaction is rolled back to undo the effect of its write operations on the
database.
Terminated state:
Therefore, I/O activity can be done in parallel with processing at the CPU.
The parallelism of the CPU and the I/O system can therefore be exploited to run
multiple transactions in parallel.
All of this increases the throughput of the system—that is, the number of
transactions executed in a given amount of time. Correspondingly, the processor
and disk utilization also increase.
There may be a mix of transactions running on a system, some short and some
long. If transactions run serially, a short transaction may have to wait for a
preceding long transaction to complete, which can lead to unpredictable delays in
running a transaction.
The database system must control the interaction among the concurrent
transactions to prevent them from destroying the consistency of the database. It
does so through a variety of mechanisms called concurrency-control schemes.
The concept of schedules to help identify those executions that are guaranteed to
ensure the isolation property and thus database consistency.
Types of Schedules:
1. Serial Schedule
2. Non-serial Schedule
3. Recoverable Schedule
4. Non-recoverable Schedule
5. Cascadeless Schedule
6. Strict Schedule
9.2. Schedules
9.2.1 Serial Schedule:
A schedule S is serial if, the transactions in the schedule are executed one after
the other(not interleaved).
Example:
Consider the schedule S with two transactions T1 and T2.
Schedule1 : T1 is followed by T2 Schedule2 : T2 is followed by T1
Example:
In this schedule,
• T2 reads the value of A updated by T1. (T2 is dependent on T1)
• T1 is committed before T2 gets committed.
• So, T2 is safe from rollback due to failure of T1.
• Thus, this is a recoverable schedule.
Example:
Consider the schedule S with two transactions T1 and T2.
In this schedule,
• So, T2 may suffer from inconsistency due to failure of T1 after a point where T2
commits.
Consider the schedule S with three transactions T1,T2 and T3 for understanding
Cascading Rollback.
In this schedule,
Cascadeless Schedule:
A schedule S is cascadeless if, every transaction in the schedule reads only the
data items that were written by committed transactions.
Cascading rollbacks will not occur in a cascadeless schedule since it reads committed
data items.
Example:
Consider the schedule S with three transactions T1,T2 and T3.
9.2. Schedules
In this schedule, the transaction reads the value A of a committed transaction.
There is no possibility of cascading rollback.
A schedule S is strict if, the transactions in the schedule can neither read nor
write an item A until the last transaction that wrote A has committed.
Example:
• In this, the transaction T2 ,reads and writes the value A of the committed
transaction T1.
Conflict serializability
View serializability
Let us consider a schedule S in which there are two consecutive instructions, I and
J, of transactions Ti and Tj, respectively ( i != j ).
If the two consecutive instructions I and J refer to different data items, then we can
swap I and J without affecting the results of any instruction in the schedule.
If the two consecutive instructions I and J 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:
I and J conflict (i.e. order of I and J matters) if they are operations by different
transactions on the same data item, and at least one of these instructions is a write
operation.
9.3. Serializability
Conflict Equivalence:
• If a schedule S can be transformed into a schedule S’ by a series of
swaps of non-conflicting instructions, we say that S and S’ are
conflict equivalent
Conflict Serializability:
• A schedule S is conflict serializable if it is conflict equivalent to a serial
schedule.
Schedule S can be transformed into Serial schedule S’, by a series of swaps of non-
conflicting instructions such as:
1. Read(B) of T1 and Write(A) of T2 are non-conflicting and swapped
9.3. Serializability
2. Read(B) of T1 and Read(A) of T2 are non-conflicting and swapped
If the two consecutive instructions I and J refer to different data items, then we
can swap I and J without affecting the results of any instruction in the schedule.
This graph consists of a pair G = (V, E), where V is a set of vertices and E is a set
of edges.
The set of vertices consists of all the transactions participating in the schedule.
The set of edges consists of all edges Ti →Tj for which one of three conditions
holds:
If an edge Ti →Tj exists in the precedence graph, then, in any serial schedule S’
equivalent to S, Ti must appear before Tj.
If the precedence graph for S has a cycle, then schedule S is not conflict
serializable.
The topological sorting for a directed acyclic graph is the linear ordering of
vertices. For every edge U->V of a directed graph, the vertex U will come before
vertex U in the ordering. Topological sort starts with a node which has zero
indegree (i.e) no incoming edges
9.3. Serializability
Example 1:
The Precedence Graph for the Schedule S contains the following edges:
If an edge Ti →Tj exists in the precedence graph, then, in any serial schedule S’
equivalent to S, Ti must appear before Tj.
The precedence graph for S does not form a cycle, then schedule S is conflict
serializable.
The Precedence Graph for the Schedule S contains the following edges:
T1 ->T2 because,
The precedence graph for S does not form a cycle, then schedule S is conflict
serializable.
Using Topological Sorting, the serializability order of the Schedule is identified as
T1 ->T2 (i.e) The schedule S is equivalent to a Serial Schedule in which T1 followed
by T2.
Example 3:
Consider a Schedule S with two Transactions T1 and T2 as follows:
Schedule S Precedence Graph for S
9.3. Serializability
The Precedence Graph for the Schedule S contains the following edges:
T1 ->T2 because,
T2 ->T1 because,
The precedence graph for S has a cycle, then schedule S is NOT conflict
serializable.
View equivalence:
Let S and S´ be two schedules with the same set of transactions. S and S´ are
view equivalent if the following three conditions are met, for each data item Q,
3. The transaction (if any) that performs the final write(Q) operation in
schedule S must also perform the final write(Q) operation in schedule S’.
Conditions 1 and 2 ensure that each transaction reads the same values in both
schedules and, therefore, performs the same computation.
Condition 3, coupled with conditions 1 and 2, ensures that both schedules result
in the same final system state.
9.3. Serializability
View serializability:
The above schedule S with three transactions T1, T2, T3 is view-serializable but not
conflict serializable because swapping of non-conflicting operations does not result in
conflict equivalence to the serial schedule.
BLIND WRITES:
Blind writes occurs in a schedule perform which does write operations without having
performed a read operation.
Example:
Blind writes appears in schedule S because, it performs write operations without having
performed a read operation.
9.3. Serializability
View Serializable
Conflict
Serializable
Concurrent access is quite easy if all users are just reading data. There is
no way they can interfere with one another. Though for any practical Database, it
would have a mix of READ and WRITE operations and hence the concurrency is a
challenge.
Lost Updates occur when multiple transactions select the same row and update
the row based on the value selected
Incorrect Summary issue occurs when one transaction takes summary over the
value of all the instances of a repeated data-item, and second transaction update
few instances of that specific data-item. In that situation, the resulting summary
does not reflect a correct result.
9.5 Need for Concurrency Control
• A transaction consists of many steps. Some involve I/O activity; others involve CPU
activity. The CPU and the disks in a computer system can operate in parallel.
Therefore, I/O activity can be done in parallel with processing at the CPU.
• The parallelism of the CPU and the I/O system can therefore be exploited to run
multiple transactions in parallel.
• All of this increases the throughput of the system – that is, the number of
transactions executed in a given amount of time.
• Correspondingly, the processor and disk utilization also increase in other words, the
processor and disk spend less time idle, or not performing any usually work.
• There may be a mix a transactions running on a system, some short and long
transactions.
• If transactions run serially, a short transaction may have to wait for a preceding long
transaction to complete, which can lead to unpredictable delays in running a
transaction.
• If the transactions are operating on different parts of the database, it is better to let
them run concurrently, sharing the CPU cycles and disk accesses among them.
Concurrent execution reduces the unpredictable delays in running transactions.
• Moreover, it also reduces the average response time: the average time for a
transaction to be completed after it has been submitted.
9.5 Need for Concurrency Control
The system needs to control the interaction among the concurrent transactions. This
control is achieved using concurrent-control schemes.
3. Timestamp protocols
Lock based Protocols helps to overcome the issues related to accessing the DBMS
concurrently by locking the current transaction for only one user. The assumption or
more like a requirement that is necessary for implementing Lock Based Protocol is
that all the data items involved are accessed in a mutually exclusive manner i.e. when
one transaction is active by a user, no other transaction is allowed access to update or
modify that transaction at the same time. As the name suggests, the Lock based
protocols when in action, are required to acquire a lock to access the data items and
release the lock when the said transaction is completed.
Types of Locks
• Binary locks
Binary Locks
A binary lock can have two states or values: locked and unlocked (or 1 and 0, for
simplicity)
A distinct lock is associated with each database item X.
If the value of the lock on X is 1, item X cannot be accessed by a database
operation that requests the item.
If the value of the lock on X is 0, the item can be accessed when requested, and
the lock value is changed to 1. We refer to the current value (or state) of the
lock associated with item X as lock(X).
In its simplest form, each lock can be a record with three fields:
<Data_item_name, LOCK, Locking_Transaction> plus a queue for transactions
that are waiting to access the item.
9.6 Locking Protocols - Two Phase Locking
The system needs to maintain only these records for the items that are currently
locked in a lock table, which could be organized as a hash file on the item name. Items
not in the lock table are considered to be unlocked. The DBMS has a lock manager
subsystem to keep track of and control access to locks. Every transaction must obey
the following rules:
2. A transaction T must issue the operation unlock_item(X) after all read_item(X) and
write_item(X) operations are completed in T.
3. A transaction T will not issue a lock_item(X) operation if it already holds the lock on
item X.
4. A transaction T will not issue an unlock_item(X) operation unless it already holds the
lock on item X.
9.6 Locking Protocols - Two Phase Locking
Shared/Exclusive locks
There are various modes in which a data item may be locked. Two modes are given
below:
1. Shared Mode
2. Exclusive Mode
When we use the shared/exclusive locking scheme, the system must enforce the
following rules:
1. A transaction T must issue the operation read_lock (X) or write_lock (X) before any
read_item(X) operation is performed in T.
2. A transaction T must issue the operation write_lock (X) before any write_item (X)
operation is performed in T.
3. A transaction T must issue the operation unlock(X) after all read_item(X) and
write_item(X) operations are completed in T.3
4. A transaction T will not issue a read_lock (X) operation if it already holds a read
(shared) lock or a write (exclusive) lock on item X.
Locking and unlocking operations for two mode (read-write or shared-exclusive) locks
9.6 Locking Protocols - Two Phase Locking
Banking Example
Consider again the banking example. Let A and B be two accounts that are accessed
by transactions T1 and T2. Transaction T1 transfers $50 from account B to account A
Transaction T2 displays the total amount of money in accounts A and B—that is, the
sum A + B. This scenario is shown below.
9.6 Locking Protocols - Two Phase Locking
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.
For example, transactions T3 and T4 are two phase. On the other hand,
transactions T1 and T2 are not two phase.
The point in the schedule where the transaction has obtained its final lock (the end
of its growing phase) is called the lock point of the transaction.
9.6 Locking Protocols - Two Phase Locking
A transaction is said to follow the two-phase locking protocol if all locking operations
(read_lock, write_lock) precede the first unlock operation in the transaction.
During the expanding phase which new locks on items can be acquired but none can
be released
During shrinking phase which existing locks can be released but no new locks can be
acquired. If lock conversion is allowed, then upgrading of locks (from read-locked to
writelocked) must be done during the expanding phase, and downgrading of locks
(from write-locked to read-locked) must be done in the shrinking phase. Hence, a
read_lock(X) operation that downgrades an already held write lock on X can appear
only in the shrinking phase.
(a) Two transactions T1 and T2. (b) Results of possible serial schedules of T1 and
T2.
9.6 Locking Protocols - Two Phase Locking
• Transactions T1 and T2 do not follow the two-phase locking protocol because the
write_lock(X) operation follows the unlock(Y) operation in T1, and similarly the
write_lock(Y) operation follows the unlock(X) operation in T2.
• The transactions can be rewritten as T1’ and T2’. Transactions T1’ and T2’, which are
the same as T1 and T2 but follow the two-phase locking protocol.
It can be proved that, if every transaction in a schedule follows the two-phase locking
protocol,
• The locking protocol, by enforcing two-phase locking rules, also enforces serializability.
Two-phase locking may limit the amount of concurrency that can occur in a schedule
because a transaction T may not be able to release an item X after it is through using
it if T must lock an additional item Y later; or conversely, T must lock the additional
item Y before it needs it so that it can release X.
• Hence, X must remain locked by T until all items that the transaction needs to read or
write have been locked; only then can X be released by T. Meanwhile, another
transaction seeking to access X may be forced to wait, even though T is done with X;
conversely, if Y is locked earlier than it is needed, another transaction seeking to
access Y is forced to wait even though T is not using Y yet.
• This is the price for guaranteeing serializability of all schedules without having to
check the schedules themselves.
• Although the two-phase locking protocol guarantees serializability (that is, every
schedule that is permitted is serializable), it does not permit all possible serializable
schedules (that is, some serializable schedules will be prohibited by the protocol).
9.6 Locking Protocols - Two Phase Locking
Types of Two-Phase Locking:
• Basic
• Conservative
• Strict
•Rigorous Two-Phase Locking
There are a number of variations of two-phase locking (2PL). The technique just
described is known as basic 2PL.
Conservative 2PL :
A variation known as conservative 2PL (or static 2PL) requires a transaction to lock all
the items it accesses before the transaction begins execution, by predeclaring its read-
set and write-set. The read-set of a transaction is the set of all items that the
transaction reads, and the write-set is the set of all items that it writes. If any of the
predeclared items needed cannot be locked, the transaction does not lock any item;
instead, it waits until all the items are available for locking. Conservative 2PL is a
deadlock-free protocol
This protocol requires not only that locking be two phase, but also that all exclusive-
mode locks taken by a transaction be held until that transaction commits. This
requirement ensures that any data written by an uncommitted transaction are locked
in exclusive mode until the transaction commits, preventing any other transaction from
reading the data.
A more restrictive variation of strict 2PL is rigorous 2PL, which also guarantees strict
schedules. In this variation, a transaction T does not release any of its locks (exclusive
or shared) until after it commits or aborts, and so it is easier to implement than strict
2PL.
Lock conversions
A transaction that already holds a lock on item X is allowed under certain conditions to
convert the lock from one locked state to another.
Types
1. Upgrade
2. Downgrade
• A mechanism can be provided for upgrading a shared lock to an exclusive lock, and
downgrading an exclusive lock to a shared lock.
• We denote conversion from shared to exclusive modes by upgrade, and from exclusive
to shared by downgrade.
• Lock conversion cannot be allowed arbitrarily. Rather, upgrading can take place in only
the growing phase, whereas downgrading can take place in only the shrinking phase.
9.7 Deadlock
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.
There are two principal methods for dealing with the deadlock problem. We can use a
deadlock prevention protocol to ensure that the system will never enter a deadlock
state. Alternatively, we can allow the system to enter a deadlock state, and then try to
recover by using a deadlock detection and deadlock recovery scheme.
Example:
A simple example is shown in below Figure, where the two transactions T1’and T2’ are
deadlocked in a partial schedule; T1’ is in the waiting queue for X, which is locked by
T2’, while T2’ is in the waiting queue for Y, which is locked by T1’. Meanwhile, neither
T1’ nor T2’ nor any other transaction can access items X and Y.
Illustrating the deadlock problem. (a) A partial schedule of T1’ and T2’ that is in a
state of deadlock. (b) A wait-for graph for the partial schedule in (a).
9.7 Deadlock
1. Deadlock Prevention
2. Deadlock Detection
3. Deadlock Recovery
1. Deadlock Prevention
Deadlock prevention protocol to ensure that the system will never enter a deadlock
state.
One way to prevent deadlock is to use a deadlock prevention protocol. One deadlock
prevention protocol, which is used in conservative two-phase locking, requires that
every transaction lock all the items it needs in advance . If any of the items cannot be
obtained, none of the items are locked. Rather, the transaction waits and then tries
again to lock all the items it needs. Obviously this solution further limits concurrency.
A second protocol, which also limits concurrency, involves ordering all the items in the
database and making sure that a transaction that needs several items will lock them
according to that order. This requires that the programmer (or the system) is aware of
the chosen order of the items, which is also not practical in the database context. A
number of other deadlock prevention schemes have been proposed that make a
decision about what to do with a transaction involved in a possible deadlock situation:
Some of these techniques use the concept of transaction timestamp TS(T), which is a
unique identifier assigned to each transaction. The timestamps are typically based on
the order in which transactions are started; hence, if transaction T1 starts before
transaction T2, then TS(T1) < TS(T2). Notice that the older transaction (which starts
first) has the smaller timestamp value.
9.7 Deadlock
A. Wait-Die Scheme
B. Wound-Wait Scheme
The major problem with both of these schemes is that unnecessary rollbacks may occur.
Another simple approach to deadlock prevention is based on lock timeouts. In this
approach, a transaction that has requested a lock waits for at most a specified amount of
time. If the lock has not been granted within that time, the transaction is said to time
out, and it rolls itself back and restarts. If there was in fact a deadlock, one or more
transactions involved in the deadlock will time out and roll back, allowing the others to
proceed.
9.7 Deadlock
2. Deadlock Detection
If a system does not employ some protocol that ensures deadlock freedom, then a
detection and recovery scheme must be used. An algorithm that examines the state of
the system is invoked periodically to determine whether a deadlock has occurred. If
one has, then the system must attempt to recover from the deadlock.
• Provide an algorithm that uses this information to determine whether the system
has entered a deadlock state.
• Recover from the deadlock when the detection algorithm determines that a deadlock
exists.
This edge is removed only when transaction Tj is no longer holding a data item
needed by transaction Ti .
A deadlock exists in the system if and only if the wait-for graph contains a cycle. Each
transaction involved in the cycle is said to be deadlocked. To detect deadlocks, the
system needs to maintain the wait-for graph, and periodically to invoke an algorithm
that searches for a cycle in the graph
9.7 Deadlock
Since the graph has no cycle, the system is not in a deadlock state.
The edge T20 → T19 is added to the wait-for graph, resulting in the new system state
shown in Figure.
This wait-for graph contains the cycle: T18 → T20 → T19 → T18.
This scenario implying that transactions T18, T19, and T20 are all deadlocked.
9.7 Deadlock
3. Deadlock Recovery
When a detection algorithm determines that a deadlock exists, the system must recover from
the deadlock. The most common solution is to roll back one or more transactions to break
the deadlock. Three actions need to be taken:
A) Selection of a victim.
a. How long the transaction has computed, and how much longer the transaction will
compute before it completes its designated task.
c. How many more data items the transaction needs for it to complete.
B) Rollback.
Once we have decided that a particular transaction must be rolled back, we must determine
how far this transaction should be rolled back. The simplest solution is a total rollback:
Abort the transaction and then restart it. However, it is more effective to roll back the
transaction only as far as necessary to break the deadlock. Such partial rollback requires
the system to maintain additional information about the state of all the running transactions.
The sequence of lock requests/grants and updates performed by the transaction needs to be
recorded. The deadlock detection mechanism should decide which locks the selected
transaction needs to release in order to break the deadlock. The selected transaction must be
rolled back to the point where it obtained the first of these locks, undoing all actions it took
after that point. The recovery mechanism must be capable of performing such partial
rollbacks. Furthermore, the transactions must be capable of resuming execution after a
partial rollback.
C) Starvation.
In a system where the selection of victims is based primarily on cost factors, it may happen
that the same transaction is always picked as a victim. As a result, this transaction never
completes its designated task, thus there is starvation. We must ensure that a transaction
can be picked as a victim only a (small) finite number of times. The most common solution is
to include the number of rollbacks in the cost factor.
9.8 Transaction Recovery , Save Points & Isolation Levels
Transaction Recovery
Recovery techniques are heavily dependent upon the existence of a special file known as
a system log. It contains information about the start and end of each transaction and
any updates which occur in the transaction. The log keeps track of all transaction
operations that affect the values of database items. This information is needed to
recover from transaction failure.
The log is kept on disk start_transaction(T): This log entry records that transaction T
starts the execution.
• read_item(T, X): This log entry records that transaction T reads the value of database
item X.
• commit(T): This log entry records that transaction T has completed all accesses to the
database successfully and its effect can be committed (recorded permanently) to the
database.
• checkpoint: Checkpoint is a mechanism where all the previous logs are removed from
the system and stored permanently in a storage disk. Checkpoint declares a point before
which the DBMS was in consistent state, and all the transactions were committed.
A transaction T reaches its commit point when all its operations that access the database
have been executed successfully i.e. the transaction has reached the point at which it
will not abort (terminate without completing). Once committed, the transaction is
permanently recorded in the database. Commitment always involves writing a commit
entry to the log and writing the log to disk. At the time of a system crash, item is
searched back in the log for all transactions T that have written a start_transaction(T)
entry into the log but have not written a commit(T) entry yet; these transactions may
have to be rolled back to undo their effect on the database during the recovery process.
9.8 Transaction Recovery , Save Points & Isolation Levels
All changes made after a savepoint has been declared can be undone by issuing a
ROLLBACK TO SAVEPOINT name command.
Issuing the commands ROLLBACK or COMMIT will also discard any savepoints
created since the start of the main transaction.
Issuing the commands ROLLBACK or COMMIT will also discard any savepoints
created since the start of the main transaction.
Oracle rolls back only the statements run after the savepoint creation.
Oracle preserves the specified savepoint, but all savepoints that were
established after the specified one are lost.
Oracle releases all table and row locks acquired since that savepoint but
retains all data locks acquired previous to the savepoint.
SAVEPOINT Adam_sal;
UPDATE employees SET salary = 12000 WHERE last_name = 'Mike';
SAVEPOINT Mike_sal;
SELECT SUM(salary) FROM employees;
ROLLBACK TO SAVEPOINT Adam_sal;
UPDATE employees SET salary = 11000 WHERE last_name = 'Mike';
COMMIT;
9.9 SQL Facilities for Concurrency and Recovery
SQL Facilities for Concurrency
Different Isolation levels can be set in SQL as follow:
READ UNCOMMITTED :
In SQL, it can be set as:
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
READ COMMITTED (default):
In SQL, it can be set as:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
REPEATABLE READ :
In SQL, it can be set as:
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
SERIALIZABLE :
In SQL, it can be set as:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
Repeatable Read : For a transaction, the data of the table cannot be modified from any
other sessions till transaction is completed
Transaction 1:
BEGIN TRANSACTION
ROLLBACK;
Transaction2:
BEGIN TRANSACTION
END TRANSACTION
If we run the above two transactions concurrently, then the update command in
Transaction2 will wait till Transaction1 is completed because T1 has been set with
REPEATABLE READ Isolation Level.
9.9 SQL Facilities for Concurrency and Recovery
• Transaction commit is done when all the operations should be done and recorded.
• Transaction abort is done when some failure occurs and none of the values updated
by the transaction should be reflected in the database.
• COMMIT
• ROLLBACK
• SAVEPOINT
SAVEPOINT Adam_sal;
UPDATE employees SET salary = 12000 WHERE last_name = 'Mike';
SAVEPOINT Mike_sal;
SELECT SUM(salary) FROM employees;
ROLLBACK TO SAVEPOINT Adam_sal; // Roll back the previous two updates
UPDATE employees SET salary = 11000 WHERE last_name = 'Mike';
COMMIT;
10. ASSIGNMENTS
1. Consider the following schedules. The actions are listed in the order they are
scheduled, and prefixed with the transaction name.
ii) Is the schedule conflict-serializable? If so, what are all the conflict equivalent serial
schedules?
iii) Is the schedule view-serializable? If so, what are all the view equivalent serial
schedules?
Solution:
The actions listed out for Schedule S1 and S2 are be written as
Schedule S1 Schedule S2
T1 T2 T3 T1 T2 T3
R(z) R(y)
R(y) R(z)
W(y) R(x)
R(y) W(x)
R(z) W(y)
R(x) W(z)
W(x) R(z)
W(y) R(y)
W(z) W(y)
R(x) R(y)
R(y) W(y)
W(y) R(x)
W(x) W(x)
10. ASSIGNMENTS
(i) PRECEDENCE GRAPH
Schedule S1
The Precedence graph for Schedule S1 consists of the following edges
T2 ->T3 because,
T2 executes R(z) before T3 executes W(z)
T2 executes R(y) before T3 executes W(y)
T2 executes W(y) before T3 executes W(y)
T2->T1 because,
T2 executes R(y) before T1 executes W(y)
T3->T1 because,
T3 executes W(y) before T1 executes R(y)
T3 executes R(y) before T1 executes W(y)
T1->T2 because,
T1 executes R(x) before T2 executes W(x)
T1 executes W(x) before T2 executes W(x)
Schedule S2
The Precedence graph for Schedule S2 consists of the following edges
T3 ->T1 because,
T3 executes R(y) before T1 executes W(y)
T3 executes W(y) before T1 executes W(y)
T3->T2 because,
T3 executes R(y) before T2 executes W(y)
T3 executes W(y) before T2 executes W(y)
T3 executes W(z) before T2 executes R(z)
T1->T2 because,
T1 executes R(x) before T2 executes W(x)
T1 executes W(x) before T2 executes W(x)
T1 executes W(x) before T2 executes R(x)
T1 executes R(y) before T2 executes R(y)
T1 executes W(y) before T2 executes W(y)
10. ASSIGNMENTS
If the precedence graph for a schedule contains a cycle, the schedule is not
Conflict Serializable.
If the precedence graph for a schedule does not contain cycle, the schedule is
Conflict Serializable.
Schedule S1
Schedule S1
Schedule S2
Schedule S1
The Schedule S1 is not conflict serializable and and does not contain Blind Writes,
so it is not View Serializable
Schedule S2
2. Consider the following schedules. The actions are listed in the order they are
Scheduled
Solution:
The actions listed out for Schedule S1 and S2 are be written as
Schedule S1 Schedule S2
T1 T2 T3 T1 T2 T3
R(x) R(x)
R(x) R(x)
W(x)
W(x)
R(x)
R(x)
W(x) W(x)
10. ASSIGNMENTS
Schedule S1
The Precedence graph for Schedule S1 consists of the following edges
T1 ->T3 because,
T1 executes R(x) before T3 executes W(x)
Schedule S2
The Precedence graph for Schedule S2 consists of the following edges
T3 ->T1 because,
T3 executes R(x) before T1 executes W(x)
T2->T1 because,
T2 executes R(x) before T1 executes W(x)
T2->T3 because,
T2 executes R(x) before T3 executes W(x)
10. ASSIGNMENTS
If the precedence graph for a schedule contains a cycle, the schedule is not
Conflict Serializable.
If the precedence graph for a schedule does not contain cycle, the schedule is
Conflict Serializable.
Schedule S1
Schedule S2
This is determined using topological sorting (start with vertex with indegree=0)
10. ASSIGNMENTS
Solution:
(a)r1(X), r3(X), w1(X), r2(X), w3(X).
There are two cycles. It is not conflict serializable.
Solution:
(c ) r3(X), r2(X), w3(X), r1(X), w1(X).
There are NO cycles. This schedule is serializable
• 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
3 List the Desirable Properties of Transactions CO3 K1
A transaction T reaches its commit point when all its operations that
access the database have been executed successfully and the effect
of all the transaction operations on the database have been
recorded in the log.
11. Part A Question & Answer
Unit – 3 TRANSACTIONS
S.No Question and Answers CO K
5 What are the two types of transaction? CO3 K1
A binary lock can have two states or values: locked and unlocked (or
1 and 0, for simplicity). A distinct lock is associated with each
database item X. If the value of the lock on X is 1, item X cannot be
accessed by a database operation that requests the item. If the
value of the lock on X is 0, the item can be accessed when
requested, and the lock value is changed to 1.We refer to the
current value (or state) of the lock associated with item X as
lock(X).
17 Explain the modes in which data item may be locked. CO3 K1
There are two modes in which data item can be locked, they are:
(i) shared –if a transaction Ti obtains this lock on an item, then it
can read the item but not write
(ii) exclusive – if a transaction obtains this lock on an item, then it
can read as well as write item.
18 Define Deadlock CO3 K1
10 Consider the following schedules. The actions are listed in the order CO3 K1
they are scheduled, and prefixed with the transaction name.
S1: T2:R(Z), T2:R(Y), T2:W(Y), T3:R(Y), T3:R(Z), T1:R(X),
T1:W(X), T3:W(Y), T3:W(Z), T2:R(X), T1:R(Y) , T1:W(Y), T2:W(X)
S2: T3:R(Y), T3:R(Z), T1:R(X), T1:W(X), T3:W(Y), T3:W(Z),
T2:R(Z), T1:R(Y), T1:W(Y), T2:R(Y), T2:W(Y), T2:R(X), T2:W(X)
For each of the schedules, answer the following questions:
i) What is the precedence graph for the schedule?
ii) Is the schedule conflict-serializable? If so, what are all the
conflict equivalent serial schedules?
iii) Is the schedule view-serializable? If so, what are all the view
equivalent serial schedules?
11 Consider the following schedules. The actions are listed in the order CO3 K1
they are scheduled
S1: R1(X), R3(X), W1(X), R2(X), W3(X)
S2: R3(X), R2(X), W3(X), R1(X), W1(X)
For each of the schedules, answer the following questions:
What is the precedence graph for the schedule?
i)Which of the following are conflict serializable schedule , Find the
equivalent serial schedule
13. SUPPORTIVE ONLINE CERTIFICATION COURSES
Data Base
Management https://nptel.ac.in/noc/courses/noc18/S
1. NPTEL
System EM1/noc18-cs15/
Database
https://www.coursera.org/learn/databas
2. Coursera Management
e-management
Essentials
Relational
https://www.coursera.org/lear
3. Coursera database
n/relational-database
systems
Database
Management https://www.udemy.com/course/databas
4. Udemy
System e-management-system/
Introduction to
Udemy Database https://www.udemy.com/course/databas
5.
Engineering e-engines-crash-course/
Databases
6. edX Courses https://www.edx.org/learn/databases
14.REAL TIME APPLICATIONS IN DAY TO DAY LIFE AND
TO INDUSTRY
1. Finances
From the stock market to your local bank, databases are abundant across the financial
world. Tracking the vast amount of information behind the world’s daily transactions
requires extremely powerful databases. This includes financial models that analyze
that data to predict future activity.
Everyday Transactions
Most of us use databases daily and may not realize it. By understanding real-world,
daily activities, you will gain a better understanding of database usage and will be
able to better apply these concepts to understand the corporate world.
A couple of everyday transactions with which everyone is familiar are
Getting a prescription filled
Using a bank machine
Getting a Prescription Filled
The example walks you through a process everyone does at some point—having a
prescription filled.
As you are standing in your local pharmacy waiting for your prescription, you may not
think that this transaction is database intensive. But, if you were to take a closer look,
hundreds of gigabytes of data, maybe terabytes, are involved in this routine
transaction.
When you evaluate a transaction like this one, it is helpful to look at one layer of the
transaction at a time, peeling away successive layers, just like peeling an onion. Figure
shows the "layers" of this transaction that you will review.
Fig: Transaction
layers for a
prescription
purchase.
14.REAL TIME APPLICATIONS IN DAY TO DAY LIFE AND
TO INDUSTRY
The final database to be reviewed at the pharmacy is the order database. This
database contains all the outstanding orders that have been placed with all the
wholesalers. They may represent simple inventory replenishment orders or special
orders for drugs not normally stocked. If the orders are for narcotic items, or
Schedule 2 drugs, special order and tracking requirements must be met to satisfy the
requirements of the Drug Enforcement Administration (DEA). Figure shows the
database entities involved at the pharmacy layer.
Timestamps
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 ).
To implement this scheme,we associate with each data item Q two timestamp
values:
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.
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.
c. Otherwise, the system executes the write operation and sets W-timestamp(Q) to
TS(Ti ).
issuance of either a read or write operation, the system assigns it a new timestamp
The protocol can generate schedules that are not recoverable. However,
it can be extended to make the schedules recoverable, in one of several
ways:
Thomas Write Rule provides the guarantee of serializability order for the protocol.
It improves the Basic Timestamp Ordering Algorithm.
16. ASSESSMENT SCHEDULE
TEXT BOOKS:
REFERENCES:
2) Inventory Management
The project starts by adding a seller and by adding details of customer. the user can now
purchase new products by the desired seller and then can sell them to the customer, the
purchasing and selling of products is reflected in the inventory section. The main aim of
Inventory Management Mini DBMS project is to add new products and sell them and keep an
inventory to manage them.
Add features like providing the menu of the food items that are prepared in the hotel. Provide
the menu and prices of the food in the hotel. you can add the online food ordering facility to
this which will give a good impression on the project. we can also book the tables in the
project. Add a feature that will show the collection of the day this will be only viewed by the
admin. we can also provide online booking of rooms also. Improve this with some other
features.
Thank you
Disclaimer:
This document is confidential and intended solely for the educational purpose of RMK Group of
Educational Institutions. If you have received this document through email in error, please notify the
system manager. This document contains proprietary information and is intended only to the
respective group / learning community as intended. If you are not the addressee you should not
disseminate, distribute or copy through e-mail. Please notify the sender immediately by e-mail if you
have received this document by mistake and delete this document from your system. If you are not
the intended recipient you are notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.