-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.sql
51 lines (37 loc) · 1.17 KB
/
transaction.sql
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
-- Exmaple - 01
CREATE TABLE widgetInventory (
id SERIAL,
description VARCHAR(255),
onhand INTEGER NOT NULL
);
CREATE TABLE widgetSales (
id SERIAL,
inv_id INTEGER,
quan INTEGER,
price INTEGER
);
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'rock', 25 );
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'paper', 25 );
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'scissors', 25 );
START TRANSACTION;
INSERT INTO widgetSales ( inv_id, quan, price ) VALUES ( 1, 5, 500 );
UPDATE widgetInventory SET onhand = ( onhand - 5 ) WHERE id = 1;
COMMIT;
SELECT * FROM widgetInventory;
SELECT * FROM widgetSales;
START TRANSACTION;
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'toy', 25 );
ROLLBACK;
SELECT * FROM widgetInventory;
SELECT * FROM widgetSales;
---Example - 02 INSERT Query using Transaction
CREATE TABLE test (
id SERIAL,
data VARCHAR(256)
);
-- Insert 1,000 times ...
INSERT INTO test ( data ) VALUES ( 'this is a good sized line of text.' );
--- Insert 1000 times using Transaction
START TRANSACTION;
INSERT INTO test ( data ) VALUES ( 'this is a good sized line of text.' );
COMMIT;