暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第16天|事务控制

原创 惊涛拍岸 2021-12-23
304

学习目标:学习openGauss事务控制

课程内容:

事务是用户定义的一个数据库操作序列,这些操作要么全做要么全不做,是一个不可分割的工作单位

部分学习内容如下:


课程作业:

1.以默认方式启动事务1,修改事务隔离级别,查看transaction_isolation

omm=# start transaction;
START TRANSACTION
omm=# select * from pg_class limit 1;
relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | r
elallvisible | reltoastrelid | reltoastidxid | reldeltarelid | reldeltaidx | relcudescrelid | relcudescidx | relhasindex | rel
isshared | relpersistence | relkind | relnatts | relchecks | relhasoids | relhaspkey | relhasrules | relhastriggers | relhassu
bclass | relcmprs | relhasclusterkey | relrowmovement | parttype | relfrozenxid | relacl | reloptions | relrepliden
t | relfrozenxid64 | relbucket | relbucketkey
--------------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+--
-------------+---------------+---------------+---------------+-------------+----------------+--------------+-------------+----
---------+----------------+---------+----------+-----------+------------+------------+-------------+----------------+---------
-------+----------+------------------+----------------+----------+--------------+-------------------+------------+------------
18 | 2840 | 0 | 0 | 0 | 0 | 0 | t | f
| p | r | 29 | 0 | f | f | f | f | f
| 0 | f | f | n | 0 | {omm=arwdDxt/omm} | | n
--+----------------+-----------+--------------
pg_statistic | 11 | 11334 | 0 | 10 | 0 | 13689 | 0 | 18 | 476 |
| 7891 | |
(1 row)

omm=# end;
COMMIT

omm=# start transaction isolation level repeatable read read write;
START TRANSACTION
omm=# show transaction_isolation;
transaction_isolation
-----------------------
repeatable read
(1 row)

omm=# rollback;
ROLLBACK

2.以读写方式启动事务2,创建新表,修改事务为只读事务,查看transaction_read_only,并向表中插入记录

omm=# start transaction isolation level read committed read write;
omm=# START TRANSACTION
create table product(id int,name char(20));
CREATE TABLE

omm=# set local transaction isolation level read committed read only;
SET
omm=# show transaction_isolation;
transaction_isolation
-----------------------
read committed
(1 row)

omm=# insert into product values(1,'open');
ERROR: cannot execute INSERT in a read-only transaction
omm=# rollback;
ROLLBACK

3.启动事务3,对表进行增删改查,并用到创建savepoint,回滚savepoint和删除savepoint

omm=# start transaction;
START TRANSACTION
omm=# insert into products values(0);
INSERT 0 1
omm=# savepoint my_point;
omm=# SAVEPOINT
insert into products values(1);
INSERT 0 1
omm=# rollback to savepoint my_point;
ROLLBACK
omm=# select * from products;
id
----
0
(1 row)

omm=# release savepoint my_point;
RELEASE
omm=# insert into products values(5);
INSERT 0 1
omm=# select * from products;
id
----
0
5
(2 rows)

omm=# commit;
COMMIT

4.清理数据

omm=# drop table products;
DROP TABLE

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论