Table "public.city" で、トランザクション

testdb=# \d city
                Table "public.city"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 no     | integer |           |          | 
 jpn    | text    |           |          | 
 eng    | text    |           |          | 
testdb=# BEGIN;
BEGIN
testdb=# INSERT INTO city VALUES(1, '青森', 'Aomori'), (2, '秋田', 'Akita');
INSERT 0 2
testdb=# DELETE FROM city WHERE no = 2;
DELETE 1
testdb=# COMMIT;
COMMIT
testdb=# INSERT INTO city VALUES(3, '札幌', 'Sapporo');
INSERT 0 1
testdb=# ROLLBACK;
WARNING:  there is no transaction in progress
ROLLBACK
testdb=# INSERT INTO city VALUES(4, '盛岡', 'Morioka');
INSERT 0 1
testdb=# ABORT;
WARNING:  there is no transaction in progress
ROLLBACK
testdb=# SELECT count(*) FROM city;
 count 
-------
     3
(1 row)

自動コミットがONになっている場合は、明示的にトランザクションを開始しない限り、SQLが処理された時点でその内容はコミットされる。

この記事が気に入ったらサポートをしてみませんか?