Docsity

ACID Properties in Relational Databases: Atomicity, Consistency, Isolation, Durability, Lecture notes of Computer science

Partial preview of the text

Download ACID Properties in Relational Databases: Atomicity, Consistency, Isolation, Durability and more Lecture notes Computer science in PDF only on Docsity!

ACID

Atomicity, Consistency, Isolation and Durability in

Relational Database Systems

Agenda

● What is a Transaction?

● Atomicity

● Isolation

● Consistency

● Durability

Transaction

● A collection of queries

● One unit of work

● E.g. Account deposit (SELECT, UPDATE, UPDATE)

Transaction Lifespan

● Transaction BEGIN

● Transaction COMMIT

● Transaction ROLLBACK

● Transaction unexpected ending = ROLLBACK (e.g. crash)

Transaction

Summary

What is a Transaction?

Atomicity

● All queries in a transaction must succeed.

● If one query fails, all prior successful queries in the transaction

should rollback.

● If the database went down prior to a commit of a transaction,

all the successful queries in the transactions should rollback

Atomicity

 - 1 $ ACCOUNT_ID BALANCE - 2 $ 
  • Send $100 From Account 1 to Account - SELECT BALANCE FROM ACCOUNT WHERE ID = - UPDATE ACCOUNT SET BALANCE = BALANCE - 100 WHERE ID = - UPDATE ACCOUNT SET BALANCE = BALANCE + 100 WHERE ID = - BALANCE >
  • BEGIN TX
    • COMMIT TX - $ - $ - $ - $ - 1 $ ACCOUNT_ID BALANCE - 2 $
  • Send $100 From Account 1 to Account
    • SELECT BALANCE FROM ACCOUNT WHERE ID =
      • UPDATE ACCOUNT SET BALANCE = BALANCE - 100 WHERE ID =
    • BALANCE >
  • BEGIN TX - $

Summary

Atomicity

Isolation

Isolation - Read phenomena

● Dirty reads

● Non-repeatable reads

● Phantom reads

● Lost updates

BEGIN TX
COMMIT TX
BEGIN TX
PID QNT PRICE

Product 1 10 $

Product 2 20 $

SALES
SELECT PID, QNT*PRICE FROM SALES
UPDATE SALES SET QNT = QNT+
WHERE PID =
SELECT SUM (QNT*PRICE) FROM SALES

Product 1, 50 Product 2, 80

We get $155 when it should be $ We read a “dirty” value that has not been committed

Dirty Reads

ROLLBACK TX
BEGIN TX
COMMIT TX
BEGIN TX
COMMIT TX
PID QNT PRICE

Product 1 10 $

Product 2 20 $

SALES
SELECT PID, QNT*PRICE FROM SALES
INSERT INTO SALES

VALUES (‘Product 3’, 10, 1)

SELECT SUM (QNT*PRICE) FROM SALES

Product 1, 50 Product 2, 80

We get $140 when it should be $ We read a committed value that showed up in our range query

Phantom read

Product 3Product 3 1010 $1 1

BEGIN TX
COMMIT TX
BEGIN TX
PID QNT PRICE

Product 1 10 $

Product 2 20 $

SALES
UPDATE SALES SET QNT = QNT+
WHERE PID =
UPDATE SALES SET QNT = QNT+
WHERE PID =
SELECT SUM (QNT*PRICE) FROM SALES

We get $155 when it should be $ Our update was overwritten another transaction and as a result “lost”

Lost updates

COMMIT TX