DBMS 3
DBMS 3
-- -
-- p ame city
custom er _Id - ----
John New York
S,1rah London
'2
f~"bert Paris
:i
Intersect · My SQ L
custom er _Id name
l city
John
J New York
london
2 '>.trah
1
h', ,t)e'"t ~ ,HIS
-l
name city
custome r _id
- - - . - -
--- - ---
Introduction to Transaction
• Examples
ount to another.
u Mo ney transfer from one acc o = 123 ;
DA TE Ac cou nt SE T bal anc e = balance - I00 WHERE AccN
UP ;
Ac cou nt SE T ba lan ce= bal ance + 100 WHERE AccNo = 124
UPDATE
□ Pu rch ase of an Item stock
ory ma nag em ent , it inv olv es creation of orders , decrease the
In invent
quantity, add ing payment, etc.
ion
B as ic Operations in a Transa. ct
Rea ds, Writes dat a item s .
X into program variable .
Rea d(X ) : Rea ds a data bas e item into the data base item X.
e of pro gra m vari able
: Wr ite( X) : Writes the valu
rt
• Spe cial actions: Com mit , Abo
· I . ·t
0
Com mit : 1og'.ca um . nnanently saved and become part of
• On suc cess ful completionnge of all the iru:~ ction as a pe
all the cha s mad e within the transact ion are
• It ens ures that .
the data base. res.
ble and can survive system failu
• Com mit ted data is made dura
all the instructions . .
0 . r.,ace s problems in complet ing
or Rol lba ck: sac tion whi ch transaction .
Ab ort . f · g tran d all the changes made within the
• Ten nin ano n o an ong oinlso known as roll bac k, un oes
. transaction began.
h d b e to its state before the
• Abo rtin g a tran sact ion, a dur ing the transac tion exe cution.
discards the cha nge s and reve rts t e ata as r. ·1ure s
• It
. d ·
10 s ons e to erro.rs or ,a, h committed to the database.
one re P .
• Abo rt is typ1caUy that no partial or mco ns1s tent c ang es are
• Rollback ens ures
4:30 ,111 5G 18 ·
IAcID Properties
■ ACID is an Atomicity, Consistency, Isolation, and Durability.
• These properties are crucial for ensuring the reliability and integrity of
transactions in a database system must maintain.
■ Atomicity:
::J Atomicity guarantees that a transaction is treated as a single, indivisible unit
of work.
'.J Either all the operations within a transaction are successfully completed and
committed, or none of them are.
□ If any part of the transaction fails, all the changes made by that transaction
are rolled back, ensuring that the database remains in a consistent state.
ACID Properties
■ Consistency:
□ Consistency ensures that a transaction brings the database from one valid state to
another.
□ The database must satisfy certain integrity constraints before and after the transaction.
□ In other words, a transaction should preserve the overall correctness and validity of
the data.
■ Isolation:
u Isolation ensures that concurrent transactions do not interfere with each other.
□ Each transaction should be executed as if it were the only transaction running on the
system.
□ Isolation prevents issues such as dirty reads, non-repeatable reads, and phantom reads.
u lt provides a level of separation between transactions to maintain data integrity and
prevent data inconsistencies.
4:30 ,111 5G 18 '
ACID Properties
■ Durability:
:::i Durability guarantees that once a transaction is committed, its changes
are permanent and will survive any subsequent system failures, such as
power outages or crashes.
'.J The committed data is stored in non-volatile storage (usually disk) and
can be recovered even in the event of a system failure.
Example Transaction
■ Transfer Rs.SO from account A to account B. The transaction will look like the
follow;
Atomicity
Read(A)
shouldn't take money from A
A=A-50 without giving it to B
Write(A) Consistency
Read(B) money isn't lost or gained
B = 8+50 Isolati-on
Write(B) other queries shouldn't see A or
B change until completion
Durability
the money does not go back to
A
Transactions in Oracle
• Transactions can consist of one or more SQL commands.
• In Oracle, a transaction starts when you connect to sq/plus, and ends when you type
COMM IT ;
::i If you're accessing the database from 2 different applications, this explains what you've altered in
one application may not show up in the second.
• An implicit COMMIT occurs before and after any Data Definition commands
(CREATE, ALTER etc)
■ You can also use SET AUTOCOMM IT ON (or OFF) to force a commit after each
command.
Read(A) At om ic ity
A= A- 50 sh ou ldn 't ta ke mo ne y
fro m A
with ou t giv ing it to B
Write(A)
Co ns ist en cy
Read(B)
mo ne y isn 't los t or ga
B = 8+ 50 ined
Is ol at io n
Write(B)
ot he r qu er ies sh ou ldn
't see A or
B change un til co mp let
ion
Du ra bi lit y
th e mo ne y does no t go
ba ck to
A
Transactions in Oracle
• Transactions ca n cons
ist of on e or more SQ L co m m an
ds.
(' ln Or ac le, a transactio
n starts when yo u co nn ec
CO M M IT ; t to sq/plus , an d en ds wh
en yo u ty pe
.J If yo u'r e accessing the da
tabase from 2 different ap
on e applicat ion may no t sho plications, this explains wh
w up in the sec on d. at you' ve altered in
An implicit CO M MIT oc
cu rs before and after any
(C RE AT E, AL TE R etc) Da ta Definition co mm an
ds
• Yo u ca n also us e Sl 'T J\L
1 O( OM M IT O l\ (o r O
command. IT) to force a co mm it af
ter ea ch
:J to co nt ro l concurrent ac
cess. Gi ve s a singl e us er
us er of th e da ta base th e ill us io n of be in g th e
sole
.J to m an ag e recovery fro
m sy ste m cr as he s