1. 트랜잭션 설정.


BEGIN;

UPDATE accounts SET balance = balance - '1000000' WHERE name = 'Alice';

SAVEPOINT save_location;

UPDATE accounts SET balance = balance + '1000000' WHERE name = 'Bob';

ROLLBACK TO save_location;

UPDATE accounts SET balance = balance + '1000000' WHERE name = 'Sam';

COMMIT;



2. 전체 sql



DROP TABLE IF EXISTS accounts;


CREATE TABLE accounts (

  name VARCHAR(20) PRIMARY KEY,

  balance MONEY

);


INSERT INTO accounts VALUES('Alice', '5000000');

INSERT INTO accounts VALUES('Bob', '500000');

INSERT INTO accounts VALUES('Sam', '1000000');


BEGIN;

UPDATE accounts SET balance = balance - '1000000' WHERE name = 'Alice';

SAVEPOINT save_location;

UPDATE accounts SET balance = balance + '1000000' WHERE name = 'Bob';

ROLLBACK TO save_location;

UPDATE accounts SET balance = balance + '1000000' WHERE name = 'Sam';

COMMIT;


SELECT * FROM accounts;


반응형

'Programming > Databse' 카테고리의 다른 글

[PostgreSql] 암호화(Encryption)  (0) 2017.06.15
[PostgreSql] 상속(Inheritance)  (0) 2017.06.15
[PostgreSql] 제약조건(Constraint) 설정, 제거  (0) 2017.06.15
[PostgreSql] 뷰(View)  (0) 2017.06.15
[PostgreSql] 기본 쿼리  (0) 2017.06.15

+ Recent posts