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 ;

六 总结

相关推荐
潘yi.2 小时前
NoSQL之Redis配置与优化
数据库·redis·nosql
zdkdchao2 小时前
hbase资源和数据权限控制
大数据·数据库·hbase
伤不起bb2 小时前
NoSQL 之 Redis 配置与优化
linux·运维·数据库·redis·nosql
leo__5202 小时前
PostgreSQL配置文件修改及启用方法
数据库·postgresql
萌新小码农‍2 小时前
Spring框架学习day7--SpringWeb学习(概念与搭建配置)
学习·spring·状态模式
蓝婷儿2 小时前
6个月Python学习计划 Day 15 - 函数式编程、高阶函数、生成器/迭代器
开发语言·python·学习
行云流水剑2 小时前
【学习记录】深入解析 AI 交互中的五大核心概念:Prompt、Agent、MCP、Function Calling 与 Tools
人工智能·学习·交互
一弓虽3 小时前
zookeeper 学习
分布式·学习·zookeeper
苗老大3 小时前
MMRL: Multi-Modal Representation Learning for Vision-Language Models(多模态表示学习)
人工智能·学习·语言模型