当前位置:网站首页>【MYSQL】事务的理解
【MYSQL】事务的理解
2022-06-25 07:54:00 【小艾同学~】
1.什么是事务
事务其实是一个完整的业务逻辑。是一个最小的工作单位不可再分。
2.怎么使用事务
事务只有DML语句才能使用,其他语句和事务无关,因为只有DML语句是数据库表中数据进行增、删、改的。只要你的操作一旦涉及到数据的增、删、改,那么就一定要考虑安全问题。
3.事务的具体使用
1.事务的默认状态
事务的默认状态为主动提交状态,每当我们执行一条DML语句后就会自动帮我们自动提交sql语句,无法回滚。
2.事务的处理状态
事务的处理状态分为两种:1.提交(commit)、2.回滚(rollback)
#事务的使用如下:
START TRANSACTION; #开启事务(将mysql的状态设置为手动提交)
INSERT INTO tab_identity(ID,`NAME`,`money`) VALUES (NULL,'小猪',500); #随便执行一条DML语句
#处理状态
ROLLBACK; #回滚
#COMMIT #提交
* 4.事务的特性
事务中一共有4个特性分别为:
A:原子性
说明事务是最小的工作单位。不可再分。
C:一致性
所有事务要求,在同一个事务当中,所有的操作必须同时成功,或者同时失败,以保证数据的一致性。
I:隔离性
A事务和B事务之间具有一定的隔离
A事务在操作一张表的时另一个事务B也操作这张表会怎么样。
D:持久性
事务最终结束的一个保障,事务提交,就相当于将没有保存到硬盘上的数据保存到硬盘上!
* 5.事务的隔离级别
事务和事务之间的隔离级别有哪些呢?4个隔离
1.读未提交:read uncommitted (最低隔离级别) 《未提交都能读取到》
什么是读未提交?
事务A可以读取到事务B未提交的数据,这种隔离级别存在的问题就是:脏读现象!(Dirty read)我们称读到了脏数
2.读已提交:read committed《提交后才能读取到》
什么是读已提交
事务A只能读取到事务B提交之后的数据,这种隔离级别解决了脏读现象。这种隔离级别不可重复读取数据,每一次读到的数据是绝对真实。
3.可重复读:repeatable read 《提交之后也读取不到,永远读取的都是刚开启事务时的数据》
事务A开启后,不管多久,每一次都在事务A中读取到的数据,不会受外界影响,即使数据已经发生变化,事务A读取到的数据还是不会变化,这就是可重复读。解决了不可重复读取数据,但是每一次读取到的数据都是幻象,不够真实!
4 .序列化/串行化:serializable (最高隔离级别)
这是最高隔离级别效率最低,解决了所有的问题,这种隔离级别表示事务排队,不能并发
查看隔离级别
-- 查看隔离级别
select @@tx_isolation;
-- 修改隔离级别
set GLOBAL TRANSACTION ISOLATION LEVEL 隔离级别;
边栏推荐
- 声纹技术(三):声纹识别技术
- 35岁腾讯员工被裁员感叹:北京一套房,存款700多万,失业好焦虑
- Trendmicro:apex one server tools folder
- Is there no risk in the security of new bonds
- iframe简单使用 、获取iframe 、获取iframe 元素值 、iframe获取父页面的信息
- Iframe is simple to use, iframe is obtained, iframe element value is obtained, and iframe information of parent page is obtained
- Exchange: manage calendar permissions
- A 35 year old Tencent employee was laid off and sighed: a suite in Beijing, with a deposit of more than 7 million, was anxious about unemployment
- 某视频网站m3u8非感知加密分析
- NIPS 2014 | Two-Stream Convolutional Networks for Action Recognition in Videos 阅读笔记
猜你喜欢

C language "recursive series": recursive implementation of 1+2+3++ n

对常用I/O模型进行比较说明

GPU calculation

Unity Addressable批量管理

C language: count the number of characters, numbers and spaces

检测点是否在多边形内

Index analysis of DEMATEL model

Find the nearest common ancestor (Sword finger offer) of two nodes in the binary tree (search tree)

Analysis of a video website m3u8 non perceptual encryption

初识生成对抗网络(11)——利用Pytorch搭建WGAN生成手写数字
随机推荐
How to calculate the positive and negative ideal solution and the positive and negative ideal distance in TOPSIS method?
Stimulsoft ultimate presents reports and dashboards
C language: count the number of characters, numbers and spaces
Data preprocessing: discrete feature coding method
Exchange: manage calendar permissions
How to interpret the information weight index?
城链科技平台,正在实现真正意义上的价值互联网重构!
wav文件(波形文件)格式分析与详解
How to become a software testing expert? From 3K to 17k a month, what have I done?
What is the role of software validation testing? What is the price of the confirmation test report?
Hyper-v:Hyper-v 第 1 代或第 2 代虚拟机
关于I/O——内存与CPU与磁盘之间的关系
Is it safe to open a stock account online now?
TrendMicro:Apex One Server 工具文件夹
【操作教程】TSINGSEE青犀视频平台如何将旧数据库导入到新数据库?
GPU calculation
Find the nearest common ancestor (Sword finger offer) of two nodes in the binary tree (search tree)
How to solve the 10061 error of MySQL in Linux
第十五周作业
How to calculate critical weight indicators?