当前位置:网站首页>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
边栏推荐
- 技能梳理[email protected]体感机械臂
- The AOV function of R language was used for repeated measures ANOVA (one intra group factor and one inter group factor) and interaction Plot function and boxplot to visualize the interaction
- 我的远程办公深度体验 | 社区征文
- Skill sorting [email protected]+ Alibaba cloud +nbiot+dht11+bh1750+ soil moisture sensor +oled
- 最新SCI影响因子公布:国产期刊最高破46分!网友:算是把IF玩明白了
- Foresniffer tutorial: extracting data
- R language plot visualization: use plot to visualize the prediction confidence of the multi classification model, the prediction confidence of each data point of the model in the 2D grid, and the conf
- 我在鹅厂淘到了一波“炼丹神器”,开发者快打包
- 如何解决跨域
- 马斯克推特粉丝过亿了,但他在线失联已一周
猜你喜欢

Dow Jones Industrial Average
[email protected]+adxl345+ Motor vibration + serial port output"/>Skill sorting [email protected]+adxl345+ Motor vibration + serial port output
[email protected]+阿里云+nbiot+dht11+bh1750+土壤湿度传感器+oled"/>技能梳理[email protected]+阿里云+nbiot+dht11+bh1750+土壤湿度传感器+oled

CVPR 2022 | Tsinghua & bytek & JD put forward BRT: Bridging Transformer for vision and point cloud 3D target detection

Viewing technological changes through Huawei Corps (V): smart Park

Auto SEG loss: automatic loss function design

逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展

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

Machine learning interview preparation (I) KNN

ArcGIS Pro脚本工具(5)——排序后删除重复项
随机推荐
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é
苹果5G芯片被曝研发失败,QQ密码bug引热议,蔚来回应做空传闻,今日更多大新闻在此...
腾讯云数据库工程师能力认证重磅推出,各界共话人才培养难题
Typescript – classes in Es5, inheritance, static methods
Getting started with X86 - take over bare metal control
那个程序员,被打了。
Collectors.toMap应用
MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
Implementation of monitor program with assembly language
Test memory read rate
js常见问题
R语言aov函数进行重复测量方差分析(Repeated measures ANOVA、其中一个组内因素和一个组间因素)、分别使用interaction.plot函数和boxplot对交互作用进行可视化
Jinbei LT6 is powerful in the year of the tiger, making waves
ArcGIS Pro脚本工具(6)——修复CAD图层数据源
Gd32 RT thread PWM drive function
Koreano essential creates a professional style
Eth is not connected to the ore pool
Turn to cartoon learning notes
"Hackers and painters" -- why not be stupid
05_ Node JS file management module FS