Mysql数据库事务的特性及运用
本文主要给大家简单讲讲MySQL数据库事务的特性及运用,相关专业术语大家可以上网查查或者找一些相关书籍补充一下,这里就不涉猎了,我们就直奔主题吧,希望Mysql数据库事务的特性及运用这篇文章可以给大家带来一些实际帮助。
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站建设、文成网络推广、小程序定制开发、文成网络营销、文成企业策划、文成品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供文成建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
二、MySQL,ODBC 数据库事务
2.1.多事务同时执行:
彼此之间互不影响的方式进行并行;事务之间交互,通过数据集。
START TRANSACTION: 启动事务命令 数据库只有启动了事务才允许回滚撤销等操作。
且数据的engine引擎必须是innodb,才支持事务
ROLLBACK : 回滚事务,mysql只要没有提交开启了事务都可以进行回滚操作。
COMMIT: 事务提交,事务提交后无法进行回滚操作。
若未明确启动事务:autocommit 能实现自动提交,每一个操作都直接提交;
强烈建议:明确使用事务,否则所有操作都被当成一个事务来处理,并关闭自动提交。
否则浪费mysql很多IO操作,每写一条语句都执行提交至持久性存储,很浪费资源
2.2.事务的特性:
2.2.1.Atomicity: 原子性
事务所引起的数据库操作,要不都完成,要么都不执行;
2.2.2.Consistency: 一致性
2.2.3.Isolation: 隔离性
事务调度: 事务之间影响最小
MVCC: 多版本并发控制
2.2.4.Durability: 持久性
一旦事务成功完成,系统必须保证任何故障都不会引起事务表示出不一致性;
1、事务提交之前就已经写出数据至持久性存储;
2、结合事务日志完成;
事务日志:顺序IO
数据文件:随机IO
2.3.事务的状态:
活动的: active
部分提交的: 最后一条语句执行后
失败的:
终止的:
提交的:
状态间的转换过程
2.4.事务并发执行的优势:
1、提高吞吐量和资源利用率 2、减少等待时间
2.5.事务调度: 1、可恢复调度 2、无极联调度
三、实例:
3.1. 设置自动提交开关
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)
3.2. 回滚事务
mysql> SET AUTOCOMMIT=0; #设定自动提交关闭
Query OK, 0 rows affected (0.00 sec)
mysql> commit #将之前的事务全部提交
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)
3.2. 保存点:恢复到所定义的那个保存点SAVEPOINT,保存点名称不能为纯数字。
保存点:SAVEPOINT savepoint_name; 保存以上操作为该保存点名称
回滚保存点:ROLLBACK TO savepoint_name; 回滚到该保存点之前的状态
3.2. 1.保存点实验一
mysql> START TRANSACTION; #启动事务
Query OK, 0 rows affected (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)
3.2.2.保存点实验二
3.2.2.1.设置a2、a4、a6、a8四个保存点
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 2 | HuangYaoshi | M | 63 |
| 3 | Miejueshitai | F | 72 |
| 4 | OuYangfeng | M | 76 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
9 rows in set (0.00 sec)
mysql> delete from tutors where TID = 2 ;
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a2 ;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tutors where TID = 4 ;
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a4;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tutors where TID = 6 ;
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a6;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tutors where TID = 8 ;
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a8;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 3 | Miejueshitai | F | 72 |
| 5 | YiDeng | M | 90 |
| 7 | Jinlunfawang | M | 67 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
5 rows in set (0.00 sec)
3.2.2.回滚到a4保存点OK
mysql> rollback to a4;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 3 | Miejueshitai | F | 72 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
7 rows in set (0.00 sec)
3.2.3.再回滚到a6保存点失败,该保存点已不存在
mysql> rollback to a6;
ERROR 1305 (42000): SAVEPOINT a6 does not exist
3.2.4.但再回滚到a2保存点OK
mysql> rollback to a2;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 3 | Miejueshitai | F | 72 |
| 4 | OuYangfeng | M | 76 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
8 rows in set (0.00 sec)
3.2.5.但再回滚到最原始保存点OK
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 2 | HuangYaoshi | M | 63 |
| 3 | Miejueshitai | F | 72 |
| 4 | OuYangfeng | M | 76 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
9 rows in set (0.00 sec)
3.2.6.回滚到最原始保存点后,但再回滚a8、a2R失败
mysql> rollback to a8;
ERROR 1305 (42000): SAVEPOINT a8 does not exist
mysql> rollback to a2;
ERROR 1305 (42000): SAVEPOINT a2 does not exist
Mysql数据库事务的特性及运用就先给大家讲到这里,对于其它相关问题大家想要了解的可以持续关注我们的行业资讯。我们的板块内容每天都会捕捉一些行业新闻及专业知识分享给大家的。
分享标题:Mysql数据库事务的特性及运用
网站地址:http://pwwzsj.com/article/jehspi.html