-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12-transactions.sql
More file actions
96 lines (76 loc) · 2.1 KB
/
Copy path12-transactions.sql
File metadata and controls
96 lines (76 loc) · 2.1 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
-- Lesson 12: Transactions and Savepoints
-- Goal: learn how PostgreSQL groups Superstore-style changes safely.
-- 1. Create a small transaction practice table from Superstore rows.
DROP TABLE IF EXISTS lesson_order_profit_adjustments;
CREATE TABLE lesson_order_profit_adjustments (
adjustment_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
row_id INTEGER NOT NULL UNIQUE,
order_id VARCHAR(50) NOT NULL,
product_name TEXT NOT NULL,
sales NUMERIC(12, 4) NOT NULL CHECK (sales >= 0),
profit NUMERIC(12, 4) NOT NULL,
review_status TEXT NOT NULL DEFAULT 'open',
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO lesson_order_profit_adjustments (
row_id,
order_id,
product_name,
sales,
profit
)
SELECT
row_id,
order_id,
product_name,
sales,
profit
FROM superstore
WHERE row_id IN (1, 2, 3);
-- 2. Review the starting values.
SELECT *
FROM lesson_order_profit_adjustments
ORDER BY row_id;
-- 3. Move a profit adjustment safely inside one transaction.
BEGIN;
UPDATE lesson_order_profit_adjustments
SET
profit = profit - 25.00,
updated_at = CURRENT_TIMESTAMP
WHERE row_id = 1;
UPDATE lesson_order_profit_adjustments
SET
profit = profit + 25.00,
updated_at = CURRENT_TIMESTAMP
WHERE row_id = 2;
COMMIT;
-- 4. Use a savepoint so part of a transaction can be undone.
BEGIN;
UPDATE lesson_order_profit_adjustments
SET review_status = 'reviewing'
WHERE row_id = 2;
SAVEPOINT before_risky_change;
UPDATE lesson_order_profit_adjustments
SET profit = profit - 10000.00
WHERE row_id = 3;
ROLLBACK TO SAVEPOINT before_risky_change;
UPDATE lesson_order_profit_adjustments
SET
review_status = 'approved',
updated_at = CURRENT_TIMESTAMP
WHERE row_id = 3;
COMMIT;
-- 5. Verify final values.
SELECT *
FROM lesson_order_profit_adjustments
ORDER BY row_id;
-- 6. Example rollback block for experimentation.
BEGIN;
UPDATE lesson_order_profit_adjustments
SET profit = profit + 9999.00
WHERE row_id = 1;
ROLLBACK;
-- 7. Confirm the rollback prevented the last change.
SELECT *
FROM lesson_order_profit_adjustments
ORDER BY row_id;