云数据库 GAUSSDB-SAVEPOINT:示例

时间:2023-11-15 14:50:13

示例

 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
--创建一个新表。
openGauss=# CREATE TABLE table1(a int);

--开启事务。
openGauss=# START TRANSACTION;

--插入数据。
openGauss=# INSERT INTO table1 VALUES (1);

--建立保存点。
openGauss=# SAVEPOINT my_savepoint;

--插入数据。
openGauss=# INSERT INTO table1 VALUES (2);

--回滚保存点。
openGauss=# ROLLBACK TO SAVEPOINT my_savepoint;

--插入数据。
openGauss=# INSERT INTO table1 VALUES (3);

--提交事务。
openGauss=# COMMIT;

--查询表的内容,会同时看到1和3,不能看到2,因为2被回滚。
openGauss=# SELECT * FROM table1;

--删除表。
openGauss=# DROP TABLE table1;

--创建一个新表。
openGauss=# CREATE TABLE table2(a int);

--开启事务。
openGauss=# START TRANSACTION;

--插入数据。
openGauss=# INSERT INTO table2 VALUES (3);

--建立保存点。
openGauss=# SAVEPOINT my_savepoint;

--插入数据。
openGauss=# INSERT INTO table2 VALUES (4);

--回滚保存点。
openGauss=# RELEASE SAVEPOINT my_savepoint;

--提交事务。
openGauss=# COMMIT;

--查询表的内容,会同时看到3和4。
openGauss=# SELECT * FROM table2;

--删除表。
openGauss=# DROP TABLE table2;
support.huaweicloud.com/distributed-devg-v2-gaussdb/gaussdb_v5r2c10_0601.html