DBMS
DBMS
(E/R) MODEL…..
ENTITY-RELATIONSHIP (E/R) MODEL
ENTITY-RELATIONSHIP (E/R) MODEL
• All entities in an Entity set/type have the same set of attributes. Chosen set of attributes
– amount of detail in modeling.
TYPES OF ATTRIBUTES (1/2)
• Simple Attributes
having atomic or indivisible values.
• example: Dept – a string
• PhoneNumber – a ten digit number
• Composite Attributes
having several components in the value.
• example: Qualification with components (DegreeName, Year, UniversityName)
• Derived Attributes
Attribute value is dependent on some other attribute.
• example: Age depends on DateOfBirth. So age is a derived attribute.
TYPES OF ATTRIBUTES (2/2)
• Single-valued
having only one value rather than a set of values.
for instance, PlaceOfBirth – single string value.
• Multi-valued
having a set of values rather than a single value.
for instance, CoursesEnrolled attribute for student EmailAddress attribute for student PreviousDegree
attribute for student.
• Attributes can be:
simple single-valued, simple multi-valued,
composite single-valued or composite multi-valued.
DIAGRAMMATIC NOTATION FOR ENTITIES
• entity - rectangle
• attribute - ellipse connected to rectangle
• multi-valued attribute - double ellipse
• composite attribute - ellipse connected to ellipse
• derived attribute - dashed ellipse
DOMAINS OF ATTRIBUTES
• Key – an attribute or a collection of attributes whose value(s) uniquely identify an entity in the
entity set.
• For instance,
• RollNumber - Key for Student entity set
• EmpID - Key for Faculty entity set
• HostelName, RoomNo - Key for Student entity set (assuming that each student gets to stay in
a single room)
• A key for an entity set may have more than one attribute.
• An entity set may have more than one key.
• Keys can be determined only from the meaning of the attributes in the entity type.
• Determined by the designers
RELATIONSHIPS
• When two or more entities are associated with each other, we have an instance of a
Relationship.
• E.g.: student Ramesh enrolls in Discrete Mathematics course
• Relationship enrolls has Student and Course as the
• participating entity sets.
• The maximum number of entities from E2 that an entity from E1 can possibly be
associated thru R (and vice-versa) determines the cardinality ratio of R.
•
• Four possibilities are usually specified:
• one-to-one (1:1)
• one-to-many (1:N)
• many-to-one (N:1)
• many-to-many (M:N)
CARDINALITY RATIOS
• Recursive relationship: An entity set relating to itself gives rise to a recursive relationship
• E.g., the relationship prereqOf is an example of a recursive relationship on the entity Course
• Role Names – used to specify the exact role in which the entity participates in the relationships
• Essential in case of recursive relationships
• Can be optionally specified in non-recursive cases
WEAK ENTITY SETS
•Each course has a name, number of credits and the department that offers it. A course may have
other courses as pre-requisites i.e, courses to be completed before it can be enrolled in.
•Professors also undertake research projects. These are sponsored by funding agencies and have a
specific start date, end date and amount of money given. More than one professor can be involved
in a project. Also a professor may be simultaneously working on several projects. A project has a
unique projectId.
ENTITIES - STUDENT
ENTITIES – DEPARTMENT AND COURSE
ENTITIES – PROFESSOR, PROJECT AND SECTIONS
E/R DIAGRAM SHOWING RELATIONSHIPS
DESIGN CHOICES: ATTRIBUTE VERSUS
RELATIONSHIP
Should offering department be an attribute of a course or should
we create a relationship between Course and Dept entities
called, say, offers ?
DESIGN CHOICES: ATTRIBUTE VERSUS
RELATIONSHIP
Should offering department be an attribute of a course or should
we create a relationship between Course and Dept entities
called, say, offers ?
• Later approach is preferable when the necessary entity, in this
case the Department, already exists.
DESIGN CHOICES: ATTRIBUTE VERSUS
RELATIONSHIP
Should class room be an attribute of Section or should we create
an entity called ClassRoom and have a relationship, say, meetsIn,
connecting Section and ClassRoom?
DESIGN CHOICES: ATTRIBUTE VERSUS
RELATIONSHIP
Should class room be an attribute of Section or should we create
an entity called ClassRoom and have a relationship, say, meetsIn,
connecting Section and ClassRoom?
• In this case, the option of making classRoom as an attribute of
Section is better as we do not want to give a lot of importance to
class room and make it a an entity.
DESIGN CHOICES
Relationship instance (c, p, j) in supply indicates that company c supplies a component p that is made
use of by the project j
TERNARY RELATIONSHIP
• (c,p) in canSupply, (j,p) in uses, (c,j) in serves may not together imply (c,p,j) is in supply.
Whereas the other way round is of course true.
RELATIONAL DATA MODEL
&
NOTION OF KEYS
RELATIONAL DATA MODEL
• Introduction
• Proposed by Edgar F Codd (1923-2003) in the early seventies [ Turing Award – 1981 ]
• Most of the modern DBMS use the relational data model.
• Simple and elegant model with a mathematical basis.
Led to the development of a theory of data dependencies and database design.
• Relational algebra operations –
• crucial role in query optimization and execution.
• Laid the foundation for the development of
• Tuple relational calculus and then
• Database standard SQL
RELATION SCHEME
• Consists of relation name, and a set of attributes or field names or column names. Each
attribute has an associated domain.
• Example:
• All entities in an Entity set/type have the same set of attributes. Chosen set of attributes
– amount of detail in modeling.
ANOTHER RELATION EXAMPLE
2
Rajesh CS04B125 CS3200
• Key: A set of attributes K, whose values uniquely identify a tuple in any instance. And none of the proper subsets of K has this
property
• Primary Key: One of the candidate keys chosen for indexing purposes ( More details later…)
RELATIONAL DATABASE SCHEME AND INSTANCE
• Relational database scheme: D consist of a finite no. of relation schemes and a set I of
integrity constraints.
• Integrity constraints: Necessary conditions to be satisfied by the data values in the relational
instances so that the set of data values constitute a meaningful database
• domain constraints
• key constraints
• referential integrity constraints
• Database instance: Collection of relational instances satisfying the integrity constraints.
DOMAIN AND KEY CONSTRAINTS
• Tuples in one relation, say r1(R1), often need to refer to tuples in another relation, say r2(R2)
• to capture relationships between entities
• Primary Key of R2 : K = {B1, B2, …, Bj}
• A set of attributes F = {A1, A2, …, Aj} of R1 such that dom(Ai) = dom(Bi), 1≤ i ≤ j and
• whose values are used to refer to tuples in r2
• is called a foreign key in R1 referring to R2.
The new course refers to a non-existent department and thus violates the RIC
EXAMPLE RELATIONAL SCHEME
A set of operators (unary and binary) that take relation instances as arguments and return new
relations.
Gives a procedural method of specifying a retrieval query.
Forms the core component of a relational query engine.
SQL queries are internally translated into RA expressions.
Provides a framework for query optimization.
• RA operations: select (s), project (p), cross product (´),
Transaction Control
• Specifies how transactions can be started / stopped, how a set
of concurrently executing transactions can be managed.
Authorization
• Specifies how to restrict a user / set of users to access only
certain parts of data, perform only certain types of queries etc.
DATA DEFINITION IN SQL
DOMAIN TYPES IN SQL-92 (1/2)
DOMAIN TYPES IN SQL-92 (2/2)
A B
1 a
2 b
3 c
4 c
Functional Dependency
Which of the following is True?
1->a 1->b
2 -> a
If there is a functional dependency, then if A is known, we can find B.
FD Definition
R (A , B) R
Tuple A B
t1 a b
A ⊆ R, B ⊆ R
t2 a a
Let A -> B exists
If t1 (A) = t2 (A),
Then t1 (B) = t2 (B),
FD Types
A->B
Trivial (No new element derived)
B ⊆ A
αβ- > α
{Name, Roll No, Course No} -> {Course No}
2 a 3 4 5
a 2 3 6 5
a 2 3 6 6
2 a 3 4 5
a 2 3 6 5
a 2 3 6 6
α- > β
A-> BC (Yes)
If α is unique,
DE ->C (Yes)
Then there always exists dependency
C-> DE (No) A->C
BD -> C
BC->A (Yes) ABDE -> C
FDs – Examples
Consider the scheme preRequisite(preReqCourse, courseId)
R (A B C)
A- > B
A - > BC
B -> C
(A)+ = (A B C)
(B)+ = ( B C)
(C)+ = (C)
Closure
R ( A B C D E F G)
1: A -> B
2: BC -> DE
3: AEG -> G
Find (AC)+
Using Rule 1: (AC)+ = ABC
Using Rule 2: (AC)+ = ABCDE
Rule 3 Not applicable.
R (A B C D E F)
AB -> C
BC -> AD
D- >E
CF -> B
(AB)+=?
F G
A- >C A->CD
AC -> D E->AH
E -> AD
E->H
A B C
1 a p
2 b q
3 c q
4 c r
(D)+=D R (A B C D E F)
(AD)+= ABCD AB->C
(BD)+= DC->AE
(CD)+ E->F
Insert Anomaly:
The first problem students usually identify with the EmpDept
schema is that it combines two different ideas: employee
information and department information.
But what is wrong with this?
Also that if we want to open a new Deptt. and thus want to insert
DeptName but do not have any employees so EID (Primary key)
and Name field will have no values…
Without any values in Primary Key..
How will the record exist..
So we can not Insert new DeptName is this scheme..
Insert Anomaly..?
EID Name DeptID DeptName
A01 Ali 12 CSE
A12 Eric 10 ECE
A13 Jack 12 CSE
A03 Tyler 12 ECE
Insert Anomaly
An Insert Anomaly occurs when certain attributes cannot be inserted into the
database without the presence of other attributes.
Example: of Courses and Rooms-teacher allotment schema.
e.g. we have built a new room (e.g. C321) but it has not yet been
timetabled for any courses or members of staff.
>> Inserting C321 is problem here !!
Delete Anomaly
A Delete Anomaly exists when certain attributes are lost because of the deletion of
other attributes.
Presence of such Anomaly clearly suggests that we have not designed our database
properly.
Delete Anomaly
Course_n Tutor Room Room_siz En_limit
o e
353 Smith A532 45 40
351 Smith C320 100 60
355 Clark H940 400 300
456 Turner H940 400 45
Minimizing redundancy
Minimizing the insertion, deletion, and update
anomalies.
Normalization is Principle of Decomposition
Number of Tables
Redundancy
Complexity
Third Normal Form (3NF)
Boyce-Codd Normal Form (BCNF)
Fourth Normal Form (4NF)
91.2914
36 Functional Dependencies
EmpNum EmpEmail
EmpNum EmpFname 3 different ways
EmpNum EmpLname you might see FDs
depicted
EmpEmail
EmpNum EmpFname
EmpLname
91.2914
37 Determinant
Functional Dependency
EmpNum EmpEmail
91.2914
38 Transitive dependency
Transitive dependency
Consider attributes A, B, and C, and where
A B and B C.
Functional dependencies are transitive, which
means that we also have the functional dependency
AC
We say that C is transitively dependent on A
through B.
91.2914
Transitive
39
dependency
EmpNum DeptNum
DeptNum DeptName
Codd states that the "values in the domains on which each relation is
defined are required to be atomic with respect to the DBMS."
Meaning a field should not be divided into parts with more than one kind
of data in it such that what one part means to the DBMS depends on
another part of the same field.
First Normal Form
Domain is atomic if its elements are considered to be indivisible units
Examples of non-atomic domains:
Set of names, composite attributes
Identification numbers like CS101 that can be broken up into parts
A relational schema R is in first normal form if the domains of all
attributes of R are atomic.
Roll Number Name Course
101 Modi CN
OS
102 Sonia DBMS
CAO
0-55-123456-9 Main Street Small House 714-000-0000 $22.95 0-55-123456-9 Jones 123-333-3333
Example 1
ISBN Title Price Table Scheme: {ISBN, Title, Price}
0-321-32132-1 Balloon $34.00 Functional Dependencies: {ISBN} {Title}
0-55-123456-9 Main Street $22.95
{ISBN} {Price}
0-123-45678-0 Ulysses $34.00
Example 3
AuID AuName AuPhone
1 Sleepy 321-321-1111
Table Scheme: {AuID, AuName, AuPhone}
2 Snoopy 232-234-1234
Functional Dependencies: {AuId} {AuPhone}
3 Grumpy 665-235-6532
{AuId} {AuName}
4 Jones 123-333-3333
{AuName, AuPhone} {AuID}
5 Smith 654-223-3455
6 Joyce 666-666-6666
7 Roman 444-444-4444
FD – Example
Database to track reviews of papers submitted to an academic
conference. Prospective authors submit papers for review and
possible acceptance in the published conference proceedings.
Details of the entities
Author information includes a unique author number, a name, a mailing
address, and a unique (optional) email address.
Paper information includes the primary author, the paper number, the
title, the abstract, and review status (pending, accepted,rejected)
Reviewer information includes the reviewer number, the name, the
mailing address, and a unique (optional) email address
A completed review includes the reviewer number, the date, the paper
number, comments to the authors, comments to the program
chairperson, and ratings (overall, originality, correctness, style, clarity)
FD – Example
Functional Dependencies
AuthNo AuthName, AuthEmail, AuthAddress
AuthEmail AuthNo
PaperNo Primary-AuthNo, Title, Abstract, Status
RevNo RevName, RevEmail, RevAddress
RevEmail RevNo
RevNo, PaperNo AuthComm, Prog-Comm, Date,
Rating1, Rating2, Rating3, Rating4, Rating5
R( A B C D)
FD:
AB->D
B->C
(AB)+=ABCD
AB: Candidate Key
Prime Attribute: A & B
Non-Prime Attribute: C & D
R( A B C D)
FD:
AB->D
B->C
R (A B D) R(B C)
FD: FD:
AB->D B->C
A B C
a 1 x
b 2 y
a 3 z
c 3 z
d 3 z
e 3 z
R (A B C)
FD: B->C
Candidate Key: ?
Decomposition Example A B C
a 1 x
b 2 y
a 3 z
c 3 z
d 3 z
e 3 z
R (A B C)
Candidate Key: (AB)+= ABC
FD: B->C
R (B C), FD: B->C
R (A B)
A B B C
a 1 1 x
b 2 2 y
a 3 3 z
c 3
d 3
e 3
Second Normal Form (2NF)
For a table to be in 2NF, there are two requirements
The database is in first normal form
All nonkey attributes in the table must be functionally dependent on the entire
primary key
Note: Remember that we are dealing with non-key attributes
R (A B C)
FD:
A->B
B->C
A B B C
a 1 1 x
b 1 2 y
c 1 3 z
d 2
For Transitive Dependency:
e 2 α- > β
f 3 Either α is a Super key
Or β is a Prime Attribute
g 3
To Check 3 NF
R (A B C D E) R1 (A B E)
FD: R11( A B)
A->B R12(B E)
B->E R2 (C D)
C->D R3 (A C)
Find Candidate Key:
(AC)+=ABCDE Final Decomposition:
Candidate Key: AC R11( A B)
R12(B E)
R2 (C D)
R3 (A C)
Check for 3NF
R ( A B C D E F G H I J) R1 (A D E I J)
FD: R11(A D E)
AB->C R12(D I J)
A->DE R2(B F G H)
B->F R21(B F)
F->GH R22 (G H)
D->IJ R3 (A B C)
Candidate Key: AB
Final Decomposition
R11(A D E)
R12(D I J)
R21(B F)
R22 (G H)
R3 (A B C)
Check for 3NF
R ( A B C D E F G H I J)
FD:
AB->C
A->DE
B->F
F->GH
D->IJ
Candidate Key: AB
Third Normal Form (3NF)
This form dictates that all non-key attributes of a table must be functionally
dependent on a candidate key i.e. there can be no interdependencies among
non-key attributes.
Candidate Key: AB , AC
A B C
a 1 x
b 2 y
c 2 z
c 3 w
d 3 w
e 3 w
R (A B C) R1 (C B)
FD: AB->C R2 (A C)
C->B
BCNF Leads to Lossy Decomposition.
Check for BCNF and Decompose
A B C
a 1 x
b 2 y
c 2 z
c 3 w
d 3 w
e 3 w
A C C B
a x x 1
b y y 2
c z z 2
c w w 3
d w
e w
Levels of Normalization
• DB DB
States of Transactions
• Active − In this state, the transaction is being
executed. This is the initial state of every
transaction.
Equivalence Schedules
• An equivalence schedule can be of the following types
−
Result Equivalence
• If two schedules produce the same result after
execution, they are said to be result equivalent.
• They may yield the same result for some value and
different results for another set of values. That's why
this equivalence is not generally considered significant.
T1 Followed by T2 T2 Followed by T1
Conflict Equivalence
Schedule 3 Schedule 6
Conflict Serializability (Cont.)
• Example of a schedule that is not conflict
serializable:
W(Z)
R(Y)
R(Y)
W(Y)
W(X)
W(Z)
W(X)
T1 T2
T3
Another Example
T1 T2 T3
R (X)
R(Y)
R(Y)
W(Y)
W(X)
W(X)
R(X)
W(Y)
As there is no Cycle, So Conflict Serializable.
Order: T1 – T3-T2
View Serializability
• A schedule will view serializable if it is view
equivalent to a serial schedule.
• If a schedule is conflict serializable, then it will
be view serializable.
• The view serializable which does not conflict
serializable contains blind writes.
View Equivalent
Schedule S
With 3 transactions, the total number of
possible serial schedule: = 3! = 6
S1 = <T1 T2 T3>
S2 = <T1 T3 T2>
S3 = <T2 T3 T1>
S4 = <T2 T1 T3>
S5 = <T3 T1 T2>
S6 = <T3 T2 T1>
Taking first schedule S1:
Step 1: final updation on data items
In both schedules S and S1, there is no
read except the initial read that's why
we don't need to check that
condition.
Step 2: Initial Read
The initial read operation in S is done
by T1 and in S1, it is also done by T1.
Step 3: Final Write
The final write operation in S is done
by T3 and in S1, it is also done by T3.
So, S and S1 are view Equivalent.
T1 T2
X
X √ X
T1 W T1
A R
X R
B T2
W
T2 Database R
T3
T1 T2 T1 (Start Time=2)
X
A X A
√ TS = 3
A
T2 (Start Time=4)
Lock-Based Protocols
• A lock is a mechanism to control concurrent access
to a data item.
• Data items can be locked in two modes :
1. exclusive (X) mode. Data item can be both read
as well as written. X-lock is requested using lock-X
instruction.
2. shared (S) mode. Data item can only be read. S-
lock is requested using lock-S instruction.
• Lock requests are made to concurrency-control
manager. Transaction can proceed only after
request is granted.
•
Lock-Based
Lock-compatibility matrix
Protocols (Cont.)
1: Requests
Concurrency Control Component T1
4: Granted/ Denied
2: Check 3: T/F
T1 T2 T1 T2
Lock-S (A)
Lock-S (A)
Read (A)
Read (A)
Lock-X(A)
Lock-S(A) Read (P)
• Simple Locking.
• 2 Phase Locking.
– Basic 2 Phase Locking (B2PL)
– Conservative 2 Phase Locking (C2PL)
– Strict 2 Phase Locking(S2PL)
– Rigorous 2 Phase Locking (R2PL)
Simple Locking
• Lock The Data Item before access.
• Release the Lock as soon as action is Over.
• Example:
1. Lock –X (A)
2. Read (A)
3. A=A+10
4. Write (A)
5. Unlock (A)
• When the Transaction executes serially, no issue.
• But when executed in interleaved manner may leads to inconsistent.
R(B)
R(B)
B=B+100
W(B) Unlock(B)
W(B)
Display (A +B)
Unlock (B)
T1 T2
Lock –X (A)
R(A)
A=A-100;
W(A)
Unlock(A) Lock-S (A)
R(A)
Unlock (A)
Lock-X(B)
R(B)
B=B+100
W(B)
Unlock (B) Lock-S(B)
R(B)
Unlock(B)
Display (A +B)
Pitfalls of Lock-Based Protocols (Cont.)
Lock Point
Unlock
R/W Lock
R/W
Locks Acquired Locks Released
Unlock
Lock
T1 B
A T2
• Cascading Rollback:
T1 T2 T3
Lock-X (A)
R(A)
Rollback
W(A)
U(A) Rollback
. Lock-X(A)
Rollback
. R(A)
Failure W(A)
U(A)
Lock-X(A)
R(A)
. . .
. . .
. . .
Conservative 2 Phase Locking Protocol
• Lock the Data items before Transaction Starts.
• Need early predictions of how many data
items may be required for transaction.
– T1 : [A B C D]
– Request Lock on { A B C D}
– If lock is granted operation starts.
– Otherwise it will wait further the request to be
granted.
Conservative 2 Phase Locking Protocol
Unlock
No Growing Phase
R/W
Locks Released
Unlock
Shrinking Phase
End Transaction
• Deadlock Never Occur.
• Early Prediction is difficult for practical
implementation.
• Cascading Rollback may present.
Strict Phase Locking Protocol
• Transaction does not release any of its lock Exclusive
Lock until it is committed/Aborted.
Lock Point
Unlock-S
Lock-X
R/W
Locks Acquired Locks Released
Lock-S End Transaction
Committed
Growing Phase Shrinking Phase
Unlock-X
• S2PL is Most Popular.
• Does not have cascading roll back.
• Deadlock May Present.
• Generates Strict Schedule.(Both Recoverable
and Cascade less.
• Easy Recovery.
Recoverable Schedule
• For each pair of transactions (Ti, Ti), if Tj reads the
item that was previously written by Ti, then commit
operation of Ti should appear before commit
operation Tj.
T1 T2
R1(A)
A= A+150
W(A)
R2(A)
A= A-50;
W2(A)
Commit;
Commit;
Strict Schedule
• If a value written by a transaction cannot be
read or over written by another transaction
until the transactions is either aborted or
committed.
• Every Strict Schedule is Recoverable and
Cascade less. T1 T2
R1 (A)
W1(A)
Commit;
W2(A)
R(A)
Rigorous Phase Locking Protocol
• Transaction does not release any of its Lock (Shared
and Exclusive) until it is committed or Aborted.
Lock Point End Transaction
Committed
R/W Lock-X
Unlock-S
Locks Acquired
Unlock-X
Lock-S
Start Transaction
No Shrinking Phase
Growing Phase
• Does not have cascading roll back.
• Deadlock May Present.
C2PL
B2PL
Lock Point
Unlock-S
Lock-X
R/W
Locks Acquired Locks Released
Lock-S End Transaction
Committed
Growing Shrinking Phase
S2PL Unlock-X
Phase
R2PL
Examples
T1
Lock-S(A) B2PL
R(A) C2PL
Lock-X(B) S2PL
R(A)
R2PL
R(B)
B=A+B
Unlock(A)
W(B)
Unlock(B)
T1
Lock-S(A)
R(A)
B2PL
Lock-X(B)
C2PL
Unlock(A) S2PL
R(B) R2PL
W(B)
Commit
Unlock(B)
T1
B2PL
Lock-S(A)
C2PL
R(A)
S2PL
Unlock(A)
R2PL
Lock-X(B)
R(B)
W(B)
Unlock(B)
Commit
Write Schedules Using B2PL
• Transaction 1: T1
Lock –S (A)
– Read (A)
R(A)
– Read (B) Lock –X (B)
– If A=0, B=B+1; R(B)
If A=0; B=B+1;
– Write (B); Unlock (A)
W(B)
Unlock(B)
Write Schedules Using C2PL
• Transaction 1: T1
R(B)
– If A=0, B=B+1;
If A=0; B=B+1;
– Write (B);
Unlock (A)
W(B)
Unlock(B)
Write Schedules Using S2PL
• Transaction 1: T1
Lock –S (A),
– Read (A)
R(A)
– Read (B) Lock –X(B)
– If A=0, B=B+1; R(B)
If A=0; B=B+1;
– Write (B); Unlock (A)
W(B)
Commit
Unlock(B)
Write Schedules Using R2PL
• Transaction 1: T1
Lock –S (A),
– Read (A)
R(A)
– Read (B) Lock –X(B)
– If A=0, B=B+1; R(B)
If A=0; B=B+1;
– Write (B); W(B)
Commit
Unlock(B)
Unlock (A)
Solve This
• Transaction 2:
– Read (B)
– Read (A)
– If B=0, A =A-B
– Write(A)
•Typically, timestamp values are assigned in the order in which the transactions
are submitted to the system, so a timestamp can be thought of as the transaction
start time.
•One possibility is to use a counter that is incremented each time its value is
assigned to a transaction. The transaction timestamps are numbered 1, 2, 3, . . .
in this scheme.
•A computer counter has a finite maximum value, so the system must periodically
reset the counter to zero when no transactions are executing for some short
period of time.
•Notice how this differs from 2PL, where a schedule is serializable by being
equivalent to some serial schedule allowed by the lockingprotocols.
•The algorithm must ensure that, for each item accessed by conflicting
operations in the schedule, the order in which the item is accessed does not
violate the serializability order.
•Todo this, the algorithm associates with each database item X two timestamp
(TS) values:-
1.Read_TS(X): The read timestamp of item X; this is the largest timestamp
among all the timestamps of transactions that have successfully read itemX—
that is, read _TS(X) = TS(T), where Tis the youngest transaction that has
read X successfully.
2.Write_TS(X): The write timestamp of item X; this is the largest ofall the
timestamps of transactions that have successfully written item X—that is,
write_ TS(X) = TS(T), where Tis the youngest transaction that has
written X successfully.
TS=10 TS=20 TS=30 TS=40
T1 T2 T3 T4
R1(A)
W1(A)
R3(A)
R2(A)
W4(A)
Read Time Stamp:
RTS (A) = 0;
Update by T1=10 (0<10)
RTS (A)=10;
Update by T3=30 (10<30)
RTS(A) =30;
Update by T2 =20 (30<20) X
RTS(A) =30.
Write Time Stamp:
WTS (A) = 0;
Update by T1=10 (0<10)
WTS(A) = 10;
Update by T4 =40 (10 <40)
WTS (A) =40;
BASICTIMESTAMPORDERING
•Whenever some transaction Ttries to issue a read_item(X) or a write_item(X)
operation, the basic TOalgorithm compares the timestamp of Twith read_TS(X)
and write_TS(X) to ensure that the timestamp order of transaction execution is
not violated.
a.If write_TS(X) >TS(T),then abort and roll back Tand reject the operation.
b.This should be done because some younger transaction with timestamp greater
than TS(T)—and hence after T in the timestamp ordering—has already written the
value of item X before Thad a chance to read X.
b.This should be done because some younger transaction with a timestamp greater
than TS(T)—and henceafter T in the timestamp ordering—has already read or
written the value of item X before T had a chance to write X, thus violating the
timestamp ordering.
c.If the condition in part (a) does not occur, then execute the write_item(X)
operation of Tand set write_TS(X) toTS(T).
Algorithm:
If (read_TS(X) > TS(T) Or write_TS(X) > TS(T))
{
Abort T ;
Rollback;
}
Else
{
Write (X);
write-TS(X) = TS (T)
}
TS=1 TS=2
T1 T2
R1(X)
RollBack W2(X)
R1(X)
T1 T2
transaction transaction
with smaller with larger
timestamp timestamp
T1 Q Shared
T2 P Exclusive
Q (Lock Not Granted) Exclusive
T3 Q Shared
T1 T2
As No Cycle, So No Deadlock.
T3
T4
Deadlock Prevention
(Using Time Stamp Ordering)
To prevent any deadlock situation in the system, the DBMS
aggressively inspects all the operations, where transactions are
about to execute.
T1
10 Sec