当前位置:网站首页>Mysql database foundation: TCL transaction control language
Mysql database foundation: TCL transaction control language
2022-06-30 10:43:00 【Long lasting bangbangjun】
List of articles
Business
1、 brief introduction
One or a group of sql Statements form an execution unit , This execution unit is either all executed , Or none at all .
The whole individual unit as an indivisible whole , If one of the units sql Once the statement fails to execute or produces an error , The whole unit will Roll back .
All affected data will be returned to the state before the start of the transaction ;
If all of the units sql Statements are executed successfully , Then the transaction is executed smoothly
Roll back : Simply put, undo the operation
2、 The transaction ACID attribute *
1、 Atomicity (Atomicity)
Atomicity means that a transaction is an indivisible unit of work , Either the operations in the transaction occur , Or none at all
2、 Uniformity (Consistency)
Consistency means that a transaction must change a database from one consistency state to another
3、 Isolation, (Isolation)
Transaction isolation means that the execution of one transaction cannot be interfered by other transactions , And the operations and data used within a transaction are isolated from other concurrent transactions , Concurrent transactions cannot interfere with each other
4、 persistence (Durability)
Persistence means that once a transaction is committed , It changes the data in the database permanently , Other subsequent operations and database failures should not have any impact on it
3、 The creation of transactions
Implicit transaction : The transaction has no obvious opening and closing marks . such as :insert、update、delete sentence
According to the transaction : A transaction has distinct open and end tags . Use the premise : Auto submit must be disabled first (set autocommit=0;)
Use steps :
1、 Open transaction :
# Set the auto submit function to disabled
set autocommit=0;
# Open transaction , If the above statement is used, it can be omitted
start transaction; # Optional
2、 Write... In a transaction sql sentence (select、insert、update、delete)
3、 End the business
commit; # Submit
rollback; # Roll back
4、 Concurrency issues
4.1 describe
For multiple transactions running simultaneously , When these transactions access the same data in the database , If necessary isolation mechanism is not adopted , It can lead to various concurrency problems :
(1) Dirty reading : For two things T1,T2,T1 Read has been T2
to update But after the fields that have not been submitted , if T2 Roll back , be T1 The read content is temporary and invalid .
(2) It can't be read repeatedly : For two things T1,T2,T1 Read a field , then T2 After updating this field ,T1 Read the same field again , The value read is different .
(3) Fantasy reading : For two things T1,T2,T1 Read a field from a table , then T2 In the table Insert After adding some new lines , If T1 Read the same table again , There will be more lines .
4.2 Set the isolation level to solve the concurrency problem
Set the isolation level to organize the generation of concurrency problems .
Isolation level : The degree to which a transaction is isolated from other transactions is called the isolation level , Different isolation levels should respond to different interference intensities , Higher isolation level , The better the data consistency , But the less concurrent .
Four transaction isolation levels provided by the database
(1)READ UNCOMMITTED( Read uncommitted data )
Allow transactions to read changes that are not committed by other transactions . Dirty reading 、 The problems of nonrepeatable reading and unreal reading will arise .
(2)READ COMMITTED( Read submitted data )
Only transactions are allowed to read changes that have been committed by other transactions . Avoid dirty reading , But the problems of unrepeatability and unreal reading may still arise .
(3)REPEATABLE READ( Repeatable )
Make sure that transactions can read the same value from a field multiple times . During the duration of this transaction , Prohibit other transactions from updating this field , It can avoid dirty reading and non repeatability , But the problem of unreal reading cannot be solved .
(4)SERIALIZABLE( Serialization )
Make sure that transactions can read the same rows from a table . During the duration of this transaction , Prevent other transactions from inserting into the table , Update and delete operations , All concurrency problems can be avoided , But the performance is very low .
oracle Database supported 2 Transaction isolation level in :READ COMMITED,SERIAlIZABLE. The default is READCOMMITED
mysql The database supports four isolation levels , The default is REPEATABLE READ
4.3 Set isolation level
- View the current isolation level
select @@transaction_isolation;
- Change the isolation level
Set up current mysql Isolation level of the connection
# Change the isolation level to read uncommitted Level
set session transaction isolation level read uncommitted;
Set the global isolation level of the database system
set global transaction isolation level read committed;
5、 Roll back
Use savepoint Set rollback point
set autocommit = 0;
start transaction;
delete from account where id = 25;
savepoint a; # Set the savepoint
delete from account where id = 28;
rollback to a; # Roll back to savepoint
perform rollback to a After this sentence , The operation after the specified rollback point will be automatically undone
6、 Use... In transactions delete and truncate The difference between
delete Support rollback ,truncate Rollback is not supported
边栏推荐
- June training (day 30) - topology sorting
- Gd32 RT thread PWM drive function
- GeoffreyHinton:我的五十年深度学习生涯与研究心法
- Skill combing [email protected] somatosensory manipulator
- mysql数据库基础:视图、变量
- 那个程序员,被打了。
- MATLAB image histogram equalization, namely spatial filtering
- Arm新CPU性能提升22%,最高可组合12核,GPU首配硬件光追,网友:跟苹果的差距越来越大了...
- 从0使用keil5软件仿真调试GD32F305
- 运动App如何实现端侧后台保活,让运动记录更完整?
猜你喜欢

Yixian e - commerce publie un rapport trimestriel: adhérer à la R & D et à l’investissement de la marque, réaliser un développement durable et de haute qualité

Using LVM to resize partitions

Musk has more than 100 million twitter fans, but he has been lost online for a week

Android 开发面试真题进阶版(附答案解析)

Robotframework learning notes: environment installation and robotframework browser plug-in installation

记一次实习的经历,趟坑必备(一)

Google 辟谣放弃 TensorFlow,它还活着!

Overview of currency

What is the real performance of CK5, the king machine of CKB?

今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
随机推荐
GD32 RT-Thread RTC驱动函数
js常见问题
Skill sorting [email protected]+adxl345+ Motor vibration + serial port output
CSDN daily one practice 2021.11.06 question 1 (C language)
What is the real performance of CK5, the king machine of CKB?
ArcGIS Pro脚本工具(6)——修复CAD图层数据源
Implementation of monitor program with assembly language
如何解决跨域
苹果5G芯片被曝研发失败,QQ密码bug引热议,蔚来回应做空传闻,今日更多大新闻在此...
Gd32 RT thread ota/bootloader driver function
Use keil5 software to simulate and debug gd32f305 from 0
Arm新CPU性能提升22%,最高可组合12核,GPU首配硬件光追,网友:跟苹果的差距越来越大了...
透過華為軍團看科技之變(五):智慧園區
MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
滴滴开源敏捷测试用例管理平台!
R语言plotly可视化:使用plotly可视化多分类模型的预测置信度、模型在2D网格中每个数据点预测的置信度、置信度定义为在某一点上最高分与其他类别得分之和之间的差值
Harvester ch1 of CKB and HNS, connection tutorial analysis
Foster design method
SGD has many improved forms. Why do most papers still use SGD?
Gd32 RT thread RTC driver function