当前位置:网站首页>【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 隔离级别;
边栏推荐
- How to implement a system call
- Is there any risk in making new bonds
- The city chain technology platform is realizing the real value Internet reconstruction!
- UEFI: repair efi/gpt bootloader
- atguigu----01-脚手架
- Wechat applet_ 7. Project practice, local life
- Retrieval model rough hnsw
- How to calculate the independence weight index?
- Check whether the point is within the polygon
- Discrimination of configuration, software configuration items and software configuration management items
猜你喜欢

What are the indicators of entropy weight TOPSIS method?

Bluecmsv1.6- code audit

Getting to know the generation confrontation network (11) -- using pytoch to build wgan to generate handwritten digits
![[operation tutorial] how does the tsingsee Qingxi video platform import the old database into the new database?](/img/21/08194ac26dec50c222b88f1f64f894.png)
[operation tutorial] how does the tsingsee Qingxi video platform import the old database into the new database?

Scanpy(七)基于scanorama整合scRNA-seq实现空间数据分析

Exchange:管理日历权限

The city chain technology platform is realizing the real value Internet reconstruction!

第五天 脚本与UI系统

Easyplayer streaming media player plays HLS video. Technical optimization of slow starting speed

Check whether the point is within the polygon
随机推荐
How to calculate the characteristic vector, weight value, CI value and other indicators in AHP?
某视频网站m3u8非感知加密分析
如何实现一个系统调用
What is the file that tp6 automatically executes? What does the tp6 core class library do?
Is it safe to open a stock account online now?
Is there no risk in the security of new bonds
Is there any risk in making new bonds
Retrieval model rough hnsw
UEFI: repair efi/gpt bootloader
声纹技术(四):声纹识别的工程部署
Is it really safe to pay new debts? Is it risky
2021 "Ai China" selection
Is there any risk in the security of new bonds
Bluecmsv1.6- code audit
Is the securities account given by Qiantang education business school safe? Can I open an account?
OpenFOAM:底层
GIL问题带来的问题,解决方法
软件确认测试有什么作用?确认测试报告的价格是多少?
4 raisons inconnues d'utiliser le "déplacement sûr à gauche"
Beam search and five optimization methods