java pg数据库事务回滚_PostgreSQL事务特性之ROLLBACK
一、回滚DDL
1. 回滚表的创建
postgres=# begin;
BEGIN
postgres=# create table test (id int, name text);
CREATE TABLE
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
postgres=# rollback ;
ROLLBACK
postgres=# \dt
No relations found.
2. 回滚表结构的修改
postgres=# create table test (id int, name text);
CREATE TABLE
postgres=# begin ;
BEGIN
postgres=# alter table test add column score int;
ALTER TABLE
postgres=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
score | integer |
postgres=# rollback ;
ROLLBACK
postgres=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
3. 回滚表的删除
postgres=# begin;
BEGIN
postgres=# drop table test ;
DROP TABLE
postgres=# \dt
No relations found.
postgres=# rollback ;
ROLLBACK
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
4. 回滚表的清空
postgres=# insert into test values (1, 'a');
INSERT 0 1
postgres=# select * from test ;
id | name
----+------
1 | a
(1 row)
postgres=# begin ;
BEGIN
postgres=# truncate test ;
TRUNCATE TABLE
postgres=# select * from test ;
id | name
----+------
(0 rows)
postgres=# rollback ;
ROLLBACK
postgres=# select * from test ;
id | name
----+------
1 | a
(1 row)
5. 回滚索引的创建
postgres=# begin ;
BEGIN
postgres=# create index on test (id);
CREATE INDEX
postgres=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
Indexes:
"test_id_idx" btree (id)
postgres=# rollback ;
ROLLBACK
postgres=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
6. 回滚索引的删除
postgres=# create index on test (id);
CREATE INDEX
postgres=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
Indexes:
"test_id_idx" btree (id)
postgres=# begin ;
BEGIN
postgres=# drop index test_id_idx ;
DROP INDEX
postgres=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
postgres=# rollback ;
ROLLBACK
postgres=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
Indexes:
"test_id_idx" btree (id)
注意如果索引的创建和删除加了CONCURRENTLY,则不能放在事务里
postgres=# drop index test_id_idx ;
DROP INDEX
postgres=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
postgres=# begin ;
BEGIN
postgres=# CREATE INDEX CONCURRENTLY ON test (id);
ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block
postgres=# rollback ;
ROLLBACK
postgres=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
二、回滚DML
1. 回滚insert
postgres=# select * from test ;
id | name
----+------
1 | a
(1 row)
postgres=# begin ;
BEGIN
postgres=# insert into test values (2, 'b');
INSERT 0 1
postgres=# select * from test ;
id | name
----+------
1 | a
2 | b
(2 rows)
postgres=# rollback ;
ROLLBACK
postgres=# select * from test ;
id | name
----+------
1 | a
(1 row)
2. 回滚update
postgres=# select * from test ;
id | name
----+------
1 | a
(1 row)
postgres=# begin ;
BEGIN
postgres=# update test set id = 2 where id = 1;
UPDATE 1
postgres=# select * from test ;
id | name
----+------
2 | a
(1 row)
postgres=# rollback ;
ROLLBACK
postgres=# select * from test ;
id | name
----+------
1 | a
(1 row)
3. 回滚delete
postgres=# select * from test ;
id | name
----+------
1 | a
(1 row)
postgres=# begin ;
BEGIN
postgres=# delete from test where id = 1;
DELETE 1
postgres=# select * from test ;
id | name
----+------
(0 rows)
postgres=# rollback ;
ROLLBACK
postgres=# select * from test ;
id | name
----+------
1 | a
(1 row)
java pg数据库事务回滚_PostgreSQL事务特性之ROLLBACK相关推荐
- mysql事务 mysql事务回滚 MySQL事务死锁 如何解除死锁 资金出入账
这里写目录标题 问题 什么是事务 为什么需要事务 创建账户表 插入数据 无事务资金出入账 有事务资金出入账 事务死锁 死锁出现的原因 解决事务死锁 查看表级锁 查询表锁进程 查询行级锁 杀死行锁进程 ...
- java pg数据库事务回滚_PG 事务提交与回滚
在PostgreSQL中,如果执行一个dml或ddl语句,默认一执行完就马上提交了,不能回滚,这样容易导致误操作的发生,避免这个风险的办法是关闭自动提交 1 设置\set AUTOCOMMIT off ...
- 数据库事务与事务回滚
目录 1.什么是事务? 2.事务的特性(ACID) 3.并发事务带来的问题 4.事务隔离级别 5.什么是回滚? 6.什么是事务回滚? 7.如何进行事务回滚 1.什么是事务? 事务是逻辑上的一组操作,要 ...
- mysql事务回滚是什么意思_Mysql事务提交及事务回滚是什么意思
本篇文章主要给大家介绍mysql事务提交及事务回滚的相关知识. 事务可以说是一段sql 语句的批处理,但是这个批处理是一个atom(原子) ,不可分割,要么都提交执行,要么回滚(rollback)都不 ...
- 什么是事务和事务回滚?
数据库理解事务: 为什么要有事务? 事务应用于各种场景,订单系统.银行系统 例如: 小明去某银行取钱,首先有以下几个步骤: 银行卡放入ATM机.,输入密码 连接数据库,验证账户密码 验证成功,获得用户 ...
- SqlServer 事务回滚
SqlServer 事务回滚 SQL事务 一.事务概念 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是一个不可分割的工作逻辑单元.在数据库 ...
- springboot 事务回滚
springboot 事务回滚 springboot 事务使用 springboot 事务使用 **1.**只有在开启事务的方法中出现异常,才会自动回滚,需要在service的public方法上面加上 ...
- java pg数据库事务回滚,基于Postgresql 事务的提交与回滚解析
用过oracle或mysql的人都知道在sqlplus或mysql中,做一个dml语句,如果发现做错了,还可以rollback;掉,但在PostgreSQL的psql中,如果执行一个dml,没有先运行 ...
- java 手动事务回滚_JAVA设置手动提交事务,回滚事务,提交事务的操作
我就废话不多说啦,还是直接看代码吧! /** * 设置数据库是否自动提交事务 * @param flag * @throws SQLException */ public void setAutoCo ...
最新文章
- Linux文件与目录管理(文件查阅)
- ML 05、分类、标注与回归
- 幼儿园带括号算式口诀_整理41组“数学顺口溜”+大九九乘法口诀表!给孩子们收藏...
- ubuntu下修改文件夹权限
- SAP启用检查双重Invoice功能
- 震惊!评审专家将论文拒稿后修改发表
- grunt前端构建工具使用教程
- 转自他人——————TLE之前,没有一个节点叫失败!!!
- 互联网产品初期需求确定与数据库模型设计——基于Visio和Xmind
- [vue] 怎么访问到子组件的实例或者子元素?
- Wi-Fi 6连续两年出货量国内登顶,锐捷无线靠什么这么6?
- This iPhone is running iOS 12.2(16E227),which may be supported by this version of Xcode
- 结构方程软件Lisrel 8.7 和HLM5.5
- LIU学生成绩管理系统源码【演示视频】
- 数据流系列-2-前后端数据传输技巧2
- 更改设置并对计算机自定义,电脑别乱玩 禁用Win8.1自定义设置项
- 三十分钟做一个网页游戏
- 破防 | 姥姥摇着头说:“不记得你了”
- 小米200万的新LOGO 一行代码就能修改?
- sql语句转为mysql语句_sql语句转换 sqlserver 转成mysql语句。