mysql数据更新回退_MySQL数据库(二)

事务

数据库开启事务命令

#start transaction 开启事务

#Rollback 回滚事务,即撤销指定的sql语句(只能回退insert delete update语句),回滚到上一次commit的位置

#Commit 提交事务,提交未存储的事务

#savepoint 保留点 ,事务处理中设置的临时占位符 你可以对它发布回退(与整个事务回退不同)

事务操作演示

create table account(

id int,

name varchar(32),

balance double);

insert into account values(1,"alex",8000);

insert into account values(2,"egon",2000);

#方式一: 更改数据后回滚,数据回到原来

select * from account;

+------+------+---------+

| id | name | balance |

+------+------+---------+

| 1 | alex | 8000 |

| 2 | egon | 2000 |

+------+------+---------+

start transaction; #开启事务后,更改数据发现数据变化

update account set balance=balance-1000 where id=1; #alex减去1000

select * from account;

+------+------+---------+

| id | name | balance |

+------+------+---------+

| 1 | alex | 7000 |

| 2 | egon | 2000 |

+------+------+---------+

rollback; #回滚后,发现数据回到原来

select * from account;

+------+------+---------+

| id | name | balance |

+------+------+---------+

| 1 | alex | 8000 |

| 1 | egon | 2000 |

+------+------+---------+

#方式二: 更改数据后提交

select * from account;

+------+------+---------+

| id | name | balance |

+------+------+---------+

| 1 | alex | 8000 |

| 2 | egon | 2000 |

+------+------+---------+

update account set balance=balance-1000 where id=1;

pdate account set balance=balance+1000 where id=2;

Commit;

select * from account;

+------+------+---------+

| id | name | balance |

+------+------+---------+

| 1 |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值