标签:start transaction savepoint rollback to
MySQL,ODBC 数据库事务
多事务同时执行:彼此之间互不影响的方式进行并行;事务之间交互,通过数据集。
START TRANSACTION; 启动事务命令 数据库只有启动了事务才允许回滚撤销等操作。
且数据的engine引擎必须是innodb,才支持事务
ROLLBACK 回滚事务,mysql只要没有提交开启了事务都可以进行回滚操作。
COMMIT:事务提交,事务提交后无法进行回滚操作。
如果没有明确启动事务: autocommit能实现自动提交,每一个操作都直接提交;
所以建议:明确使用事务,否则所有操作都被当成一个事务来处理,并关闭自动提交。
否则会浪费mysql很多IO操作,每写一条语句都执行提交至持久性存储,很浪费资源
事务的特性:
Atomicity:原子性,事务所引起的数据库操作,要不都完成,要么都不执行;
Consistency: 一致性,
Isolation:隔离性
事务调度:事务之间影响最小
MVCC:多版本并发控制
Durability:持久性 ,一旦事务成功完成,系统必须保证任何故障都不会引起事务表示出不一致性;
1、事务提交之前就已经写出数据至持久性存储;
2、结合事务日志完成;
事务日志:顺序IO
数据文件:随机IO
事务的状态:
活动的:active
部分提交的:最后一条语句执行后
失败的:
终止的:
提交的:
及状态间的转换过程:
事务并发执行的优势:1、提高吞吐量和资源利用率 2、减少等待时间
事务调度:1、可恢复调度 2、无极联调度
实例:
mysql> SELECT @@AUTOCOMMIT; #查询自动提交的状态1为开启,0为关闭;
+--------------+
| @@AUTOCOMMIT |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
mysql> SET AUTOCOMMIT=0; #设定自动提交关闭
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@AUTOCOMMIT; #查询自动提交的状态1为开启,0为关闭;
+--------------+
| @@AUTOCOMMIT |
+--------------+
| 0 |
+--------------+
1 row in set (0.00 sec)
mysql> DELETE FROM student WHERE Name LIKE ‘Li%‘; #删除Name字段包含Li的行
Query OK, 1 row affected (0.03 sec)
mysql> SELECT * FROM student; #Li哪行已经被删除
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 0 | 3 |
| 4 | Guo Jing | 0 | 4 |
+-----+------------+-----+-----+
3 rows in set (0.00 sec)
mysql> ROLLBACK; #回滚事务,自动提交关闭后,默认就开启了事务,可以实现回滚等操作
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM student; #删除的哪行恢复了
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 0 | 3 |
| 4 | Guo Jing | 0 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)
保存点:恢复到所定义的那个保存点SAVEPOINT,保存点名称不能为数字。
保存点:SAVEPOINT savepoint_name; 保存以上操作为该保存点名称
回滚保存点:ROLLBACK TO savepoint_name; 回滚到该保存点之前的状态
Usage:
mysql> START TRANSACTION; #启动事务
Query OK, 0 rows affected (0.00 sec)
mysql> select * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
| 4 | Guo Jing | 53 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)
mysql> SAVEPOINT a; #该保存点student表数据都存在
Query OK, 0 rows affected (0.00 sec)
mysql> select * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
| 4 | Guo Jing | 53 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)
mysql> DELETE FROM student WHERE SID=4; #删除student表的SID为4的行
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
+-----+------------+-----+-----+
3 rows in set (0.00 sec)
mysql> SAVEPOINT b; #该保存点student表数据的SID为4的行不存在了
Query OK, 0 rows affected (0.00 sec)
mysql> DELETE FROM student WHERE SID=3;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
+-----+------------+-----+-----+
2 rows in set (0.00 sec)
mysql> SAVEPOINT c; #该保存点student表的SID为3和4的行都不存在
Query OK, 0 rows affected (0.00 sec)
mysql> ROLLBACK TO b; #回滚至保存点b,即student表,SID为4的行不存在的行
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
+-----+------------+-----+-----+
3 rows in set (0.00 sec)
mysql> ROLLBACK TO a;#即所有数据都存在的那个点
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
| 4 | Guo Jing | 53 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)
本文出自 “学linux历程” 博客,请务必保留此出处http://woyaoxuelinux.blog.51cto.com/5663865/1924030
Linux命令:MySQL系列之八--MySQL事务相关内容
标签:start transaction savepoint rollback to
原文地址:http://woyaoxuelinux.blog.51cto.com/5663865/1924030