Lecture_2 database
Lecture_2 database
Processing,
and Management
Ali El-Bastawissy 1
What is a
Transaction?
Ali El-Bastawissy 2
What is a Transaction?
A database request is the equivalent of
a single SQL statement in an application
program or transaction.
A transaction that changes the contents of
the database must alter the database from
one consistent database state to another.
To ensure consistency of the
database, every transaction must
begin with the database in a known
consistent state.
Ali El-Bastawissy 3
Example of
Transaction
Amount in stock = X
Ali El-Bastawissy 4
Multi-Updates
Transaction
An accountant wishes to register the credit
sale of 100 units of product X to customer Y in
the amount of $500.00:
Reducing product X’s Quantity on hand by 100.
Adding $500.00 to customer Y’s accounts
receivable.
UPDATE PRODUCT SET PROD_QOH = PROD_QOH - 100
WHERE PROD_CODE = ‘X’;
UPDATE ACCREC SET AR_BALANCE = AR_BALANCE + 500
WHERE AR_NUM = ‘Y’;
If the above two transactions are not completely executed,
the transaction yields an inconsistent database.
Ali El-Bastawissy 5
Example of
Transaction
Amount in Items X & Y
X = 1000
Y = 7000 Initial State <Consistent State>
But
HOWwwww?
Ali El-Bastawissy 6
Transaction Management with
SQL
Transaction support is provided by 2 SQL
statements:
COMMIT
ROLLBACK
When a transaction sequence is initiated, it must
continue through all succeeding SQL
statements until one of the following four events
occurs:
A COMMIT statement is reached.
A ROLLBACK statement is reached.
The end of a program is successfully reached
(COMMIT
Like).
The program is abnormally terminated (ROLLBACK
Ali El-Bastawissy 7
Transaction Management with
SQL
Our last example:
UPDATE PRODUCT
SET PROD_QOH = PROD_QOH - 100
WHERE PROD_CODE = ‘X’;
UPDATE ACCREC
SET AR_BALANCE = AR_BALANCE +
500
WHERE AR_NUM = ‘Y’;
COMMIT;
Ali El-Bastawissy 8
A Transaction Processing
Example
Ali El-Bastawissy 9
Transaction ACID
Atomic
Properties
All operations of a transaction should be completed; if not,
the transaction is aborted.
Transactions cannot be subdivided
Consistent
Transaction must alter the database from one consistent
state to another consistent state.
Isolated
Data used during the execution of a transaction cannot be
used by a second transaction until the first one is
completed.
Durable
Database changes are permanent
The permanence of the database’s consistent state.
Ali El-Bastawissy 10
How DBMSs Manage Transactions??
Ali El-Bastawissy 11
The Transaction Log
Transaction log stores:
A record for the beginning of transaction
For each transaction operation:
○ Type of operation being performed (update,
delete, insert)
○ Names of objects affected by transaction
○ “Before” and “after” values for updated fields
○ Pointers to previous and next transaction
log entries for the same transaction
Ending (COMMIT) of the transaction
Ali El-Bastawissy 12
The information stored in the log is used by
the DBMS for a recovery requirement
triggered by a ROLLBACK statement or a
system failure.
In such cases, the DBMS returns back the
before data image to get the initial consistent
state.
The transaction log is itself a database, and it
is managed by the DBMS like any other
database.
Ali El-Bastawissy 13
Ali El-Bastawissy 14
Transaction Processing
Ali El-Bastawissy 18
Assignment
19