mysql 学习11 事务,事务简介,事务操作,事务四大特性,并发事务问题,事务隔离级别
一 事务简介,
数据库准备:
create table account(id int auto_increment primary key comment '主键ID',name varchar(128) not null comment '姓名',backaccountnumber char(18) unique comment '银行账号',money float comment '余额' )comment '银行账号表';#drop table account;insert into accountvalues (null,'张三','123412341234123412',2000),(null,'lisi','123412341234123413',8000),(null,'wangwu','123412341234123414',9000);
二 事务操作,
由于mysql 默认的一条一条sql语句都是 默认开启事务的。
当我们执行三条sql语句的时候,1,2都成功了,但是3失败了,就会造成问题
这三条sql语句是:张三给李四的账号转1000块
1.查询张三的账号的钱 大于1000块
2. 张三money = money-1000;
3, 李四money = money +1000;
事务的操作的两种方式
select @@autocommit; set @@autocommit = 0;select * from account where name = '张三'; update account set money = money-1000 where name = '张三'; update account set money = money+1000 where name = 'lisi';commit ;rollback ;
start transaction ;select * from account where name = '张三'; update account set money = money-1000 where name = '张三'; update account set money = money+1000 where name = 'lisi';commit;rollback ;