-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38_Transactions.sql
More file actions
30 lines (20 loc) · 1.24 KB
/
38_Transactions.sql
File metadata and controls
30 lines (20 loc) · 1.24 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
-- Transaction in mysql is a group of SQL statements that are executed as a single unit of work.
-- Transactions ensure data integrity and consistency by following the ACID properties: Atomicity, Consistency, Isolation, Durability.
-- Atomicity → all operations within the transaction are completed successfully or none are applied
-- Consistency → data remains in a valid state before and after the transaction
-- Isolation → concurrent transactions do not interfere with each other
-- Durability → once a transaction is committed, the changes are permanent
-- Basic Transaction Commands:
-- START TRANSACTION -> This command is used to begin a new transaction.
-- COMMIT -> This command is used to save all the changes made during the current transaction.
-- ROLLBACK -> This command is used to undo all the changes made during the current transaction.
-- SAVEPOINT -> This allows you to set a check point within a transaction to which you can later roll back if needed.
-- Example of a basic transaction:
START TRANSACTION;
INSERT INTO accounts(id, balance) VALUES (1, 500);
INSERT INTO accounts(id, balance) VALUES (2, 700);
SAVEPOINT sp1;
UPDATE accounts SET balance = balance - 200 WHERE id = 1;
-- Oops something wrong
ROLLBACK TO sp1;
COMMIT;