-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask13.sql
More file actions
146 lines (91 loc) · 2.42 KB
/
Copy pathtask13.sql
File metadata and controls
146 lines (91 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
--Create Sample Banking Table
CREATE DATABASE task13;
USE task13;
CREATE TABLE accounts (
acc_id INT PRIMARY KEY,
name VARCHAR(50),
balance DECIMAL(10,2)
);
INSERT INTO accounts VALUES
(1, 'Alice', 10000),
(2, 'Bob', 5000);
--Basic Transaction Example (Money Transfer)
--Goal: Transfer ₹2000 from Alice → Bob
START TRANSACTION;
UPDATE accounts
SET balance = balance - 2000
WHERE acc_id = 1;
UPDATE accounts
SET balance = balance + 2000
WHERE acc_id = 2;
COMMIT;
Check result:
SELECT * FROM accounts;
--Simulate Failure + ROLLBACK
--Suppose system crashes before second update
START TRANSACTION;
UPDATE accounts
SET balance = balance - 3000
WHERE acc_id = 1;
-- simulate error (wrong account id)
UPDATE accounts
SET balance = balance + 3000
WHERE acc_id = 99;
ROLLBACK;
Now verify:
SELECT * FROM accounts;
--Alice balance will return to original → Atomicity proven
--ACID Properties Demonstration
-- A — Atomicity
All operations succeed OR none.
Example → money transfer uses rollback if one fails.
-- C — Consistency
Database rules always valid.
Example:
ALTER TABLE accounts ADD CONSTRAINT chk_balance CHECK (balance >= 0);
--Prevents negative balances.
-- I — Isolation
Transactions do not interfere with each other.
Handled using isolation levels (shown below).
--D — Durability
After COMMIT → changes are permanent even after crash.
--Isolation Levels
SELECT @@transaction_isolation;
Set level:
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
--Types:
Level Prevents
READ UNCOMMITTED Nothing
READ COMMITTED Dirty reads
REPEATABLE READ Non-repeatable reads
SERIALIZABLE Phantom reads
--MySQL default → REPEATABLE READ
--PART 6 — Dirty Read Demo (2 Sessions)
--Session A
START TRANSACTION;
UPDATE accounts SET balance = 20000 WHERE acc_id = 1;
-- do not commit
--Session B
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT * FROM accounts;
--Sees uncommitted value → dirty read
Prevent by:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
--Concurrent Update Handling
Use row locking:
START TRANSACTION;
SELECT * FROM accounts
WHERE acc_id = 1
FOR UPDATE;
UPDATE accounts
SET balance = balance - 1000
WHERE acc_id = 1;
COMMIT;
--Prevents two users updating same row simultaneously.
--PART 8 — Banking System Mapping
Transaction = ATM transfer:
--Deduct from sender
--Add to receiver
--If network fails → rollback
--Prevents partial transfer
--Ensures money not lost