当前位置:网站首页>MySQL——事务
MySQL——事务
2022-06-11 19:32:00 【Java学不会】
1、什么是事务
- 要么都成功,要么都失败
- SQL执行 A给B 转账 A 1000 -->200 B 200
- SQL执行 B收到A的钱 A 800 --> B 400
- 将一组SQL放在一个批次中执行
- 事务原则 :ACID原则 原子性,一致性,隔离性,持久性(脏读,幻读)
- 原子性(Atomicity):要么都成功,要么都失败
- 一致性(Consistency):事务前后的数据完整保证一致
- 持久性(Durability):事务一旦提交则不可逆,被持久化到数据库中
- 隔离性(Isolation):一个事务的执行不能其它事务干扰。即一个事务内部的操作及使用的数据对其它并发事务是隔离的,并发执行的各个事务之间不能互相干扰。
- 脏读:指一个事务读取了另一个事务未提交的数据。
- 不可重复读:在一个事务内读取表中的某一行数据,多次读取结果不同。(这个不一定是错误,只是某些场合不对)
- 幻读(虚读):指在一个事务内读取到了别的事务插入饿数据,导致前后读取不一致。
2、测试事务实现转账
- 转账事务
-- 手动处理事务 SET autocommit =0 -- 事务开启 START TRANSACTION -- 标记一个事务的开始,从这个之后的SQL都在一个事务内 UPDATE account SET money=money-500 WHERE `name` = 'A' -- A减500 UPDATE account SET money=money+500 WHERE `name` = 'B' -- B加500 -- 提交:持久化(成功) COMMIT -- 回滚:回到原来的样子(失败) ROLLBACK -- 事务结束 SET autocommit = 1 -- 恢复默认值 - 事务使用的总结
-- 手动处理事务 SET autocommit =0 -- 事务开启 START TRANSACTION -- 标记一个事务的开始,从这个之后的SQL都在一个事务内 UPDATE account SET money=money-500 WHERE `name` = 'A' -- A减500 UPDATE account SET money=money+500 WHERE `name` = 'B' -- B加500 -- 提交:持久化(成功) COMMIT -- 回滚:回到原来的样子(失败) ROLLBACK -- 事务结束 SET autocommit = 1 -- 恢复默认值 SAVEPOINT 保存点名 -- 设置一个事务的保存点 ROLLBACK TO SAVEPOINT 保存点名 -- 回滚到保存点 RELEASE SAVEPOINT 保存点名 -- 释放保存点
边栏推荐
- Bottomsheetdialog usage details, setting fillet, fixed height, default full screen, etc
- leetcode:66.加一
- Learn about random library · 1
- Yolov3 pytoch code and principle analysis (II): network structure and loss calculation
- On Workflow selection
- Flutter--Button浅谈
- Introduction to go language (VI) -- loop statement
- LDPC 7 - simple example of decoding
- Practice of Flink CDC in Dajian cloud warehouse
- 【求助】请问如何让微信公众号文章在外部浏览器中打开后还能显示下方的精选留言?
猜你喜欢

Chrome tips - browser web page setting coding, solving the problem of web page garbled code, obtaining the latest version of charset plug-in, UTF-8 coding setting

Use Mysql to determine the day of the week

何恺明团队的“视频版本MAE”,高效视频预训练!Mask Ratio高达90%时效果也很好!...

Given a project, how will you conduct performance testing?

Experience of remote office communication under epidemic situation | community essay solicitation
【求助】請問如何讓微信公眾號文章在外部瀏覽器中打開後還能顯示下方的精選留言?

Golang学习笔记—基础篇

【Multisim仿真】利用运算放大器产生锯齿波
[help] how can wechat official account articles be opened in an external browser to display the selected messages below?

CMU 15-445 數據庫課程第五課文字版 - 緩沖池
随机推荐
【视频去噪】基于SALT实现视频去噪附Matlab代码
【信号去噪】基于非线性滤波器实现语音自适应去噪附matlab代码
Programmers have changed dramatically in 10 years. Everything has changed, but it seems that nothing has changed
Key contents that wwdc22 developers need to pay attention to
Qubicle notes: Hello voxel
AHB2Standard_handshake_bridge 设计
管理者必须知道的三个常识
计算926的9260次方里的字符串里有多少个926
Highcharts sets the histogram width, gradient, fillet, and data above the column
[Multisim Simulation] using operational amplifier to generate sawtooth wave
Picture bed: picgo+ Tencent cloud +typera
Longest strictly increasing subsequence
What is the workflow of dry goods MapReduce?
[image denoising] image denoising based on Markov random field with matlab code
CMU 15 - 445 cours de base de données Leçon 5 version texte - Pool tampon
[help] how can wechat official account articles be opened in an external browser to display the selected messages below?
Template and requirements of curriculum design of reinforced concrete structure in autumn 21 of Dagong [standard answer]
基于飞桨搭建的多模态学习工具包PaddleMM
MOS transistor 24n50 parameters of asemi, 24n50 package, 24n50 size
激活函数公式、导数、图像笔记