mysql事务的基本理解

文章目录

    • 1.1 事务整个流程中的关键字
    • 1.2 事务提交成功后不可回滚
    • 1.3 事务未提交进行全部回滚
    • 1.4 事务未提交回滚到某保存点

1.1 事务整个流程中的关键字

关键字

关键字 说明
begin # 等于start transaction;表示开始一个事务,建议使用start transaction;
savepoint # 设置保存点,保存点的名字不能以数字开头;正确示例:savepoint aa # 设置保存点为aa
rollback # 回滚事务到事务开始(rollbackup;),或者回滚到某个保存点(rollbackup to 保存点;)
commit # 事务提交,整个事务结束;

注意事项

注意:不受autocommit在会话模式和全局模式下值的影响;但一定得按照顺序来哈

start transaction;表示开始一个事务,若结束可以用commit命令;只要commit命令提交后,你就得重新执行start transaction;命令哈;

若一个事务中设置了保存点,但是在执行rollback时没有指定保存点,就回到了事务最开始,此时不能再回退到任何一个保存点上了;

若一个事务中设置了多个保存点(aa和bb),若进行rollbackup to aa;后,此时不可能再回到bb保存点了;

1.2 事务提交成功后不可回滚

** 环境准备(我这里是测试环境哈)**

## 创建chenliang库,并进入到chenliang库下面
create database if not exists chenliang character set utf8 collate utf8_general_ci;
show databases like "chenliang";
use chenliang;

## 创建test1表
create table if not exists test1(
	id int unsigned not null auto_increment comment"序列",
	name varchar(20) not null comment"姓名",
	age tinyint unsigned not null comment"年龄",
	primary key(id)
)engine=innodb character set utf8 collate utf8_general_ci comment"测试表1";

## 查看test1表的表结构
desc test1;

事务一但提交成功,不可再进行回滚的验证

## 开始事务
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

## 往test1表中插入数据,并提交事务
mysql> insert into test1(name,age) values("chenliang01",24); 
Query OK, 1 row affected (0.00 sec)

mysql> select * from test1; 
+----+-------------+-----+
| id | name        | age |
+----+-------------+-----+
|  1 | chenliang01 |  24 |
+----+-------------+-----+
1 row in set (0.00 sec)

mysql> commit; 
Query OK, 0 rows affected (0.00 sec)

## 现在想起插入的数据有误(当然可以修改),但是没法进行回滚了哈;

## 回滚事务,看是否会生效
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test1;
+----+-------------+-----+
| id | name        | age |
+----+-------------+-----+
|  1 | chenliang01 |  24 |
+----+-------------+-----+
1 row in set (0.00 sec)

## 实践总结
事务一但提交(commit),无法进行回滚;

1.3 事务未提交进行全部回滚

环境准备(我这里是测试环境哈)

## 创建chenliang库,并进入到chenliang库下面
create database if not exists chenliang character set utf8 collate utf8_general_ci;
show databases like "chenliang";
use chenliang;

## 创建test2表
create table if not exists test2(
	id int unsigned not null auto_increment comment"序列",
	name varchar(20) not null comment"姓名",
	age tinyint unsigned not null comment"年龄",
	primary key(id)
)engine=innodb character set utf8 collate utf8_general_ci comment"测试表2";

## 查看test2表的表结构;
desc test2;

事务未提交,回滚至事务开始的实践

## 开始1个事务
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

## 往test2表中插入数据,不提交事务哈,且查看插入到表中的数据
mysql> insert into test2(name,age) values("cl01",26);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test2; 
+----+------+-----+
| id | name | age |
+----+------+-----+
|  1 | cl01 |  26 |
+----+------+-----+
1 row in set (0.00 sec)

## 回滚事务(回滚至事务开始)
mysql> rollback; 
Query OK, 0 rows affected (0.01 sec)

mysql> select * from test2; 
Empty set (0.00 sec)

## 结束这个事务
mysql> commit;                 <==提交这个事务(之前的数据已回滚了哈)
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test2;    <==查看test2表中的数据
Empty set (0.00 sec)

# 实践总结
事务未提交(commit)可以进行回滚;

1.4 事务未提交回滚到某保存点

环境准备(我这里是测试环境哈)

## 创建chenliang库,并进入到chenliang库下面
create database if not exists chenliang character set utf8 collate utf8_general_ci;
show databases like "chenliang";
use chenliang;

## 创建test3表
create table if not exists test3(
	id int unsigned not null auto_increment comment"序列",
	name varchar(20) not null comment"姓名",
	age tinyint unsigned not null comment"年龄",
	primary key(id)
)engine=innodb character set utf8 collate utf8_general_ci comment"测试表3";

## 查看test3表的表结构;
desc test3;

事务未提交,回滚至某个保存点

## 开始事务
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

## 往test3表中插入一条数据,并设置保存点"lili"
mysql> insert into test3(name,age) values("lili",24);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test3;
+----+------+-----+
| id | name | age |
+----+------+-----+
|  1 | lili |  24 |
+----+------+-----+
1 row in set (0.00 sec)

mysql> savepoint lili;
Query OK, 0 rows affected (0.00 sec)

## 再往test3表中插入一条数据,设置保存点为"lanlan"
mysql> insert into test3(name,age) values("lanlan",24);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test3;
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
|  1 | lili   |  24 |
|  2 | lanlan |  24 |
+----+--------+-----+
2 rows in set (0.00 sec)

mysql> savepoint lanlan;
Query OK, 0 rows affected (0.00 sec)

## 回滚至保存点"lili"
mysql> rollback to lili;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test3;
+----+------+-----+
| id | name | age |
+----+------+-----+
|  1 | lili |  24 |
+----+------+-----+
1 row in set (0.00 sec)

## 提交事务
mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test3;
+----+------+-----+
| id | name | age |
+----+------+-----+
|  1 | lili |  24 |
+----+------+-----+
1 row in set (0.00 sec)

## 实践总结
事务未提交(commit),可回滚至某个保存点(前提是要设置保存点);

你可能感兴趣的:(#,mysql,5.7.X)