事务
数据库开启事务命令
#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 |