mysql 内置功能 事务 介绍
事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性
创建数据库db12
create database db12 charset=utf8;
use db12;
create table user(
id int primary key auto_increment,
name char(32),
balance int
); insert into user(name,balance)
values
('wsb',1000),
('egon',1000),
('ysb',1000);
mysql> select * from user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 4 | wsb | 1000 |
| 5 | egon | 1000 |
| 6 | ysb | 1000 |
+----+------+---------+
3 rows in set (0.00 sec)
#原子操作
start transaction; 开启事务
事务结束:End Transaction;
start transaction;
update user set balance=900 where name='wsb'; #买支付100元
update user set balance=1010 where name='egon'; #中介拿走10元
update user set balance=1090 where name='ysb'; #卖家拿到90元
commit;
commit ;提交
#出现异常,回滚到初始状态
rollback ;回滚
只要没有commit提交 就可以回滚到初始的金额
start transaction;
update user set balance=900 where name='wsb'; #买支付100元
update user set balance=1010 where name='egon'; #中介拿走10元
update user set balance=1090 where name='ysb'; #卖家拿到90元,出现异常没有拿到
rollback;
commit;
没有commit; 执行rollback;
mysql> start transaction; mysql> update user set balance=900 where name='wsb'; #买支付100元
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> update user set balance=1010 where name='egon'; #中介拿走10元
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> update user set balance=1090 where name='ysb'; #卖家拿到90元
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 1 | wsb | 900 |
| 2 | egon | 1010 |
| 3 | ysb | 1090 |
+----+------+---------+
3 rows in set (0.00 sec)
回滚到初始
mysql> rollback;
Query OK, 0 rows affected (0.12 sec) mysql> select * from user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 1 | wsb | 1000 |
| 2 | egon | 1000 |
| 3 | ysb | 1000 |
+----+------+---------+
3 rows in set (0.00 sec)
commit ; 提交后 再执行 rollback; 不能回滚了
只要不执行commit; 就不会修改数据 你都可以任意回滚回去
ysql> update user set balance=900 where name='wsb'; #买支付100元
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> update user set balance=1010 where name='egon'; #中介拿走10元
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> update user set balance=1090 where name='ysb'; #卖家拿到90元
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 4 | wsb | 900 |
| 5 | egon | 1010 |
| 6 | ysb | 1090 |
+----+------+---------+
3 rows in set (0.00 sec) mysql> commit;
Query OK, 0 rows affected (0.00 sec) mysql> select * from user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 4 | wsb | 900 |
| 5 | egon | 1010 |
| 6 | ysb | 1090 |
+----+------+---------+
3 rows in set (0.00 sec) mysql> rollback;
Query OK, 0 rows affected (0.00 sec) mysql> select * from user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 4 | wsb | 900 |
| 5 | egon | 1010 |
| 6 | ysb | 1090 |
+----+------+---------+
3 rows in set (0.00 sec)
开启事务检测sql语句的执行,如果监测到一条sql语句有错误时候,执行失败,立马所有执行回滚操作
mysql 内置功能 事务 介绍的更多相关文章
- mysql 内置功能 存储过程介绍
存储过程介绍 就是mysql内置功能把逻辑写好 的功能给封装好,封装成一个接口名,把接口名丢给应用程序,应用程序直接调用接口名实现一系列增删改查功能 这个接口叫存储过程 基于存储过程封装成一个功能 存 ...
- mysql 内置功能 视图介绍
之前的多表查询本质是把多张有关系的表连接在一起组成一张虚拟表,从而进行查询 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名], 用户使用时只需使用[名称]即 ...
- mysql 内置功能 触发器介绍
使用触发器可以在用户对表进行[增.删.改]操作时前后定义一些操作,注意:没有查询 创建触发器 create trigger 触发器的名字 之前(before)或者之后(after) 行为(inser ...
- mysql 内置功能目录
mysql 内置功能 视图介绍 mysql 内置功能 视图 使用 mysql 内置功能 触发器介绍 mysql 内置功能 触发器 实验 mysql 内置功能 事务 介绍 mysql 内置功能 存储过程 ...
- mysql 内置功能 存储过程 目录
mysql 内置功能 存储过程介绍 mysql 内置功能 存储过程 创建无参存储过程 mysql 内置功能 存储过程 创建有参存储过程 mysql 内置功能 存储过程 删除存储过程
- MySQL:记录的增删改查、单表查询、约束条件、多表查询、连表、子查询、pymysql模块、MySQL内置功能
数据操作 插入数据(记录): 用insert: 补充:插入查询结果: insert into 表名(字段1,字段2,...字段n) select (字段1,字段2,...字段n) where ...; ...
- mysql六:mysql内置功能(视图、触发器、事务、存储过程、函数)
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 通过使用视图可以把查询过程中的 ...
- MySQL内置功能之事务、函数和流程控制
主要内容: 一.事务 二.函数 三.流程控制 1️⃣ 事务 一.何谓事务? 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. # ...
- 数据库——mysql内置功能(11)
1.视图 视图是一个虚拟表(非真实存在),其本质是(根据SQL语句获取动态的数据集,并未其命名),用户使用时只需使用(名称)即可获取结果集,可以将该结果集当做表来使用 使用视图我们可以把查询过程中的临 ...
随机推荐
- QT显示中文 连接上文
1.首先是建立Linux开发环境1.1.在windowsXP下安装博创公司提供的虚拟机软件VMware Workstation,版本为VMware-workstation-full-7.0.1-227 ...
- UISegmentedControl的基本用法
本文转载至 http://www.tuicool.com/articles/yUfURj 原文 http://blog.csdn.net/hmt20130412/article/details/38 ...
- 【LeetCode OJ】Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length ...
- php计算两个日期时间差(返回年、月、日)
在PHP程序中,很多时候都会遇到处理时间的问题,比如:判断用户在线了多长时间,共登录了多少天,两个帖子发布的时间差或者是不同操作之间的日志记录等等.在文章中,简单地举例介绍了PHP中如何计算两个日期相 ...
- Python学习(23):Python面向对象(1)速成
转自 http://www.cnblogs.com/BeginMan/p/3190776.html 一.Python经典类与新类 经典类:如果没有直接或间接的子类化一个对象,也就是说如果没有指定一个父 ...
- UVA 10120 - Gift?!(搜索+规律)
Problem D. Gift?! The Problem There is a beautiful river in a small village. N rocks are arranged ...
- 【大数据系列】安装Ambari
一.Ambari简介 The Apache Ambari project is aimed at making Hadoop management simpler by developing soft ...
- jQuery().end()的内部实现及源码分析
jQuery().end()的作用是返回当前jQuery对象的上一个状态. 1.end()源码: // 所有通过pushStack方法获得的jQuery对象都可以通过end方法返回之前的状态 // ...
- ios-toolchain-based-on-clang-for-linux
https://github.com/tpoechtrager/cctools-port.git https://www.embtoolkit.org
- 第一步 使用sencha touch cmd 4.0 创建项目、打包(加入全局变量、公用类、自定义扩展、资源文件)
参考资料: http://www.cnblogs.com/qqloving/archive/2013/04/25/3043606.html http://www.admin10000.com/docu ...