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 account
    values (null,'张三','123412341234123412',2000),
           (null,'lisi','123412341234123413',8000),
           (null,'wangwu','123412341234123414',9000);

二 事务操作,

由于mysql 默认的一条一条sql语句都是 默认开启事务的。

当我们执行三条sql语句的时候,1,2都成功了,但是3失败了,就会造成问题

这三条sql语句是:张三给李四的账号转1000块

1.查询张三的账号的钱 大于1000块

  1. 张三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 ;

三 事务四大特性,

四 并发事务问题,

并发事务问题:指的是 A 事务 和 B事务 在同时 操作某一个章表的时候,发生的问题。

脏读

事务A 有三步:

start transaction ;

  1. select money from account where id =1; # 查询 id 为1的银行账户

  2. update account set money = money+30000 where id = 1; #工资发了3万

  3. update account set money = money+80000 where id = 1; # 奖金发了8万

commit;

事务B 也有三步:

start transaction ;

  1. select money from account where id =1; # 查询 id 为1的银行账户

  2. update account set money = money-10000 where id = 1; #买了个华为手机

  3. update account set money = money-30 where id = 1; # 吃了个中午饭

commit;

脏读发生的时机:事务B 读取到了 事务A 还没有commit的数据

当事务A 执行了第一步和第二步的时候,注意,这时候事务A 还没有提交

这时候事务B 执行了第一步,查询,会看到 账户已经加了3万块钱了

这个就是脏读

不可重复读

一个事务先后读取同一个数据,但是第一次 和 第二次 读取到的数据不同。

事务A 有4步:

start transaction ;

  1. select money from account where id =1; # 查询 id 为1的银行账户,发现值是1000

  2. ......

(这时候事务B 执行完3步后,提交了)

  1. select money from account where id =1; # 查询 id 为1的银行账户,发现值是2000

  2. .......

commit;

事务B 有三步:

start transaction ;

  1. select money from account where id =1; # 查询 id 为1的银行账户

  2. update account set money = 1000 where id = 1; #钱变成了1000

  3. select money from account where id =1; # 查询 id 为1的银行账户

commit;

幻读

五 事务隔离级别

那么前面遇到的 并发事务问题,应该怎么解决呢? 通过 设置 事务隔离级别 就可以解决。

复制代码
# 查看当前 事务级别 是啥
select @@transaction_isolation;


# 设置 事务级别 session 代表会话级别的,只是对当前窗口有效; global 代表全局生效,即当前窗口和其他窗口都生效
set session  transaction isolation level read uncommitted ;
set session transaction isolation level read committed ;
set session  transaction isolation level repeatable read ;
set session transaction isolation level serializable ;

六 总结

相关推荐
Kazefuku1 分钟前
Excel学习笔记
笔记·学习·excel
小浪学编程22 分钟前
C#学习——继承、封装、多态
学习
hnlucky29 分钟前
《Zabbix Proxy分布式监控实战:从安装到配置全解析》
数据库·分布式·学习·adb·zabbix·集成学习·proxy模式
Catfood_Eason39 分钟前
初识MySQL
数据库·mysql
red_redemption1 小时前
自由学习记录(59)
学习
光电大美美-见合八方中国芯1 小时前
【平面波导外腔激光器专题系列】1064nm单纵模平面波导外腔激光器‌
网络·数据库·人工智能·算法·平面·性能优化
鬼才血脉2 小时前
Ubuntu上安装MySQL 8并配置Navicat远程连接
mysql·ubuntu·adb
时序数据说2 小时前
通过Linux系统服务管理IoTDB集群的高效方法
大数据·linux·运维·数据库·开源·时序数据库·iotdb
Chef_Chen2 小时前
从0开始学习大模型--Day04--大模型的框架以及基本元素
学习
CopyLower2 小时前
解决 Redis 缓存与数据库一致性问题的技术指南
数据库·redis·缓存