博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql之commit,transaction事物控制
阅读量:6173 次
发布时间:2019-06-21

本文共 3627 字,大约阅读时间需要 12 分钟。

简单来说,transaction就是用来恢复为以前的数据。

举个例子,我想把今天输入到数据库里的数据在晚上的时候全部删除,那么我们就可以在今天早上的时候开始transaction事物,令autocommit关闭并且执行commit,然后再开始输入数据,到晚上的时候,可以执行rollback恢复到今天没输入数据的状态,也就是恢复到commit前的数据。

[root@localhost ~]# mysql -uroot -p              #登录数据库Enter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 10Server version: 5.5.52-MariaDB MariaDB ServerCopyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               |+--------------------+4 rows in set (0.00 sec)MariaDB [(none)]> create database bp                #我自己创建一个数据库用来做这个实验    -> ;Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> MariaDB [(none)]> use bp;Database changedMariaDB [bp]> create table test(id int,name varchar(20));               #建表Query OK, 0 rows affected (0.08 sec)MariaDB [bp]> insert into test values(1,'123');                         Query OK, 1 row affected (0.06 sec)MariaDB [bp]> insert into test values(2,'323');Query OK, 1 row affected (0.01 sec)MariaDB [bp]> select * from test;+------+------+| id   | name |+------+------+|    1 | 123  ||    2 | 323  |+------+------+2 rows in set (0.00 sec)MariaDB [bp]> show variables like '%commit%';                       #查看autocommit是否关闭,可以看到现在开启着+-------------------------------------------+-------+| Variable_name                             | Value |+-------------------------------------------+-------+| aria_group_commit                         | none  || aria_group_commit_interval                | 0     || autocommit                                | ON    || innodb_commit_concurrency                 | 0     || innodb_flush_log_at_trx_commit            | 1     || innodb_use_global_flush_log_at_trx_commit | ON    |+-------------------------------------------+-------+6 rows in set (0.00 sec)MariaDB [bp]> set autocommit=0;                             #关闭autocommitQuery OK, 0 rows affected (0.00 sec)MariaDB [bp]> show variables like '%commit%';+-------------------------------------------+-------+| Variable_name                             | Value |+-------------------------------------------+-------+| aria_group_commit                         | none  || aria_group_commit_interval                | 0     || autocommit                                | OFF   || innodb_commit_concurrency                 | 0     || innodb_flush_log_at_trx_commit            | 1     || innodb_use_global_flush_log_at_trx_commit | ON    |+-------------------------------------------+-------+6 rows in set (0.00 sec)MariaDB [bp]> start transaction;                            #开始事物Query OK, 0 rows affected (0.00 sec)MariaDB [bp]> delete from test where id=1;Query OK, 1 row affected (0.00 sec)MariaDB [bp]> select * from test;+------+------+| id   | name |+------+------+|    2 | 323  |+------+------+1 row in set (0.00 sec)MariaDB [bp]> commit;                                   #记录前面的数据Query OK, 0 rows affected (0.01 sec)MariaDB [bp]> delete from test where id=2;              #删除数据Query OK, 1 row affected (0.00 sec)MariaDB [bp]> select * from test;Empty set (0.00 sec)MariaDB [bp]> rollback;                                 #回滚到commit记录的数据Query OK, 0 rows affected (0.00 sec)MariaDB [bp]> select * from test;                       #回滚成功+------+------+| id   | name |+------+------+|    2 | 323  |+------+------+1 row in set (0.00 sec)MariaDB [bp]>

转载于:https://www.cnblogs.com/biaopei/p/7730509.html

你可能感兴趣的文章
Inno Setup入门(二十)——Inno Setup类参考(6)
查看>>
图片自适应
查看>>
amd cmd
查看>>
Linux下的uml画图工具
查看>>
xml返回数组数据
查看>>
约瑟夫问题总结
查看>>
spring mybatis 批量插入返回主键
查看>>
指针函数小用
查看>>
开源力量公开课第二十三期-从SVN到Git,次时代代码管理
查看>>
输入挂
查看>>
升级迁移前,存储过程统计各个用户下表的数据量,和迁移后的比对
查看>>
sql注入分类
查看>>
初识CSS选择器版本4
查看>>
[Hadoop in China 2011] 朱会灿:探析腾讯Typhoon云计算平台
查看>>
JavaScript之数组学习
查看>>
PHP 设置响应头来解决跨域问题
查看>>
CAS实现SSO单点登录原理
查看>>
博客园美化专用图片链接
查看>>
HDU_1969_二分
查看>>
高等代数葵花宝典—白皮书
查看>>