事务(TRANSACTION)是一个整体,要么一起执行,要么一起不执行。
开启事务
start transaction 或 begin [work]
提交事务
commit
回滚事务
rollback
例题:
-- 插入测试数据
mysql> create table bank(
-> card char(4) primary key comment '卡号',
-> money decimal(10,2) not null
-> )engine=innodb charset=utf8;
Query OK, 0 rows affected (0.05 sec)
mysql> insert into bank values ('1001',1000),('1002',1);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
-- 开启事务
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter // -- 更改定界符
mysql> update bank set money=money-100 where card='1001';
-> update bank set money=money+100 where card='1002' //
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-- 回滚事务
mysql> rollback //
Query OK, 0 rows affected (0.00 sec)
mysql> select * from bank //
+------+---------+
| card | money |
+------+---------+
| 1001 | 1000.00 |
| 1002 | 1.00 |
+------+---------+
2 rows in set (0.00 sec)
------------------------------------------------------------------
-- 开启事务
mysql> start transaction //
Query OK, 0 rows affected (0.00 sec)
mysql> update bank set money=money-100 where card='1001';
-> update bank set money=money+100 where card='1002' //
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-- 提交事务
mysql> commit //
Query OK, 0 rows affected (0.00 sec)
mysql> select * from bank //
+------+--------+
| card | money |
+------+--------+
| 1001 | 900.00 |
| 1002 | 101.00 |
+------+--------+
设置事务的回滚点
-- 开启事务
mysql> begin //
Query OK, 0 rows affected (0.00 sec)
mysql> insert into bank values ('1003',500) //
Query OK, 1 row affected (0.00 sec)
-- 记录事务的回滚点
mysql> savepoint a1 //
Query OK, 0 rows affected (0.00 sec)
mysql> insert into bank values ('1004',200) //
Query OK, 1 row affected (0.00 sec)
-- 回滚到回滚点
mysql> rollback to a1 //
Query OK, 0 rows affected (0.00 sec)
-- 查询
mysql> select * from bank //
+------+--------+
| card | money |
+------+--------+
| 1001 | 900.00 |
| 1002 | 101.00 |
| 1003 | 500.00 |
+------+--------+
3 rows in set (0.00 sec)
自动提交事务
每一个SQL语句都是一个独立的事务
小结:
1、事务是事务开启的时候开始
2、提交事务、回滚事务后事务都结束
3、只有innodb支持事务
4、一个SQL语句就是一个独立的事务,开启事务是将多个SQL语句放到一个事务中执行
php pdo处理事务 bank 表中有卡号和金额 两个字段
<?php
//pdo操作事务
/*
$pdo->beginTransaction() 开启事务
$pdo->commit () 提交事务
$pdo->rollBack() 回滚事务
*/
if(!empty($_POST)){
$dsn='mysql:host=localhost;port=3306;dbname=datanews;charset=utf8';
$pdo=new PDO($dsn,'root','123456');
$out=$_POST['card_out'];
$in=$_POST['card_in'];
$money=$_POST['money'];
$pdo->beginTransaction(); //开启事务
$flag1=$pdo->exec("update bank set balance=balance-$money where card='$out'");
$flag2=$pdo->exec("update bank set balance=balance+$money where card='$in'");
//查看转出的账号是否大于0,大于0返回true,否则返回false
$stmt=$pdo->query("select balance from bank where card='$out'");
$flag3=$stmt->fetchColumn()>=0?1:0;
if($flag1 && $flag2 && $flag3){
$pdo->commit();//提交事务
echo "转账成功";
}else{
$pdo->rollBack();//提交事务
echo "转账失败";
}
}
?>
<form action="" method="post">
转出卡号 <input type="text" name="card_out">
转入卡号<input type="text" name="card_in">
金额 <input type="text" name="money">
<input type="submit" name="button" value="提交">
</form>