PostgreSQL事务特性之ROLLBACK
2015-08-27 00:00
477 查看
摘要: PostgreSQL事务特性之ROLLBACK
4. 回滚表的清空
注意如果索引的创建和删除加了CONCURRENTLY,则不能放在事务里
二、回滚DML
1. 回滚insert
一、回滚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)
相关文章推荐
- PostgreSQL新手入门教程
- PostgreSQL教程(十):性能提升技巧
- PostgreSQL教程(二):模式Schema详解
- PostgreSQL教程(十三):数据库管理详解
- PostgreSQL教程(八):索引详解
- PostgreSQL教程(三):表的继承和分区表详解
- 简单介绍Ruby on Rails对PostgreSQL数组类型的支持
- PostgreSQL教程(十九):SQL语言函数
- PostgreSQL教程(四):数据类型详解
- PostgreSql新手必学入门命令小结
- 用一整天的时间安装postgreSQL NTFS权限
- FREEBSD安装POSTGRESQL笔记
- PostgreSQL8.3.3安装方法第1/2页
- 深入理解PostgreSQL的MVCC并发处理方式
- PostgreSQL教程(一):数据表详解
- PostgreSQL教程(十二):角色和权限管理介绍
- PostgreSQL教程(五):函数和操作符详解(1)
- 15个postgresql数据库实用命令分享
- Windows下Postgresql数据库的下载与配置方法