The fifth bomb of MySQL learning - detailed explanation of transactions and their operating characteristics

affairs

  • Business Profile
  • transaction operation
  • Four characteristics of business
  • concurrent transaction problem
  • transaction isolation level

A transaction is a collection of operations. It is an indivisible unit of work. A transaction will submit or revoke operation requests to the system as a whole, that is, these operations either succeed or fail at the same time.

Picture 1
picture 2
picture 3
picture 4
picture 5
picture 6

-- ---------------------------------事务操作-----------------------------------------
-- 数据准备
create table account(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    money int comment '余额'
) comment '账户表';
insert into account(id, name, money) VALUES (null, '张三', 2000), (null, '李四', 2000);

-- 恢复数据
update account set money = 2000 where name = '张三' or name = '李四';

select @@autocommit;
set @@autocommit = 1; -- 设置为手动提交

-- 转账操作(张三给李四转1000)
-- 1.查询张三余额
select * from account where name = '张三';
-- 2、将张三余额减1000
update account set money = money - 1000 where name = '张三';

-- 3、将李四余额加1000
update account set money = money + 1000 where name = '李四';

-- 提交事务
commit ;

-- 回滚事务
rollback ;

-- 方式二
-- 转账操作(张三给李四转账1000)
start transaction ;
-- 1、查询张三余额
select * from account where name = '张三';
-- 2、将张三余额-1000
update account set money = money - 1000 where name = '张三';
-- 3、将李四余额+1000
update account set money = money + 1000 where name = '李四';

-- 提交事务
commit ;
-- 回滚事务
rollback ;

-- 事务的四大特性
-- 原子性
-- 一致性
-- 隔离性
-- 持久性

-- 并发事务问题

-- 事务的隔离级别

-- 查看事务隔离级别
select @@transaction_isolation;
-- 设置事务隔离级别
set session transaction isolation level read uncommitted;
set session transaction isolation level repeatable read ;

Guess you like

Origin blog.csdn.net/loveCC_orange/article/details/124366754