当前位置:网站首页>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
边栏推荐
- Go -- standard library sort package
- 运动App如何实现端侧后台保活,让运动记录更完整?
- Ant s19xp appeared in 140t, why is it called the computing power ceiling by the world
- 那个程序员,被打了。
- Leetcode question brushing (I) -- double pointer (go Implementation)
- Viewing technological changes through Huawei Corps (V): smart Park
- Ionic4 drag the ion reorder group component to change the item order
- 最新SCI影响因子公布:国产期刊最高破46分!网友:算是把IF玩明白了
- scratch绘制正方形 电子学会图形化编程scratch等级考试二级真题和答案解析2022年6月
- SGD有多种改进的形式,为什么大多数论文中仍然用SGD?
猜你喜欢

苹果高管公然“开怼”:三星抄袭 iPhone,只加了个大屏

The latest SCI impact factor release: the highest score of domestic journals is 46! Netizen: I understand if

微信推出图片大爆炸功能;苹果自研 5G 芯片或已失败;微软解决导致 Edge 停止响应的 bug|极客头条...
[email protected]在oled上控制一条狗的奔跑"/>技能梳理[email protected]在oled上控制一条狗的奔跑

ArcGIS Pro scripting tool (6) -- repairing CAD layer data sources

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

WGet -- 404 not found due to spaces in URL
[email protected]基于51系列单片机的智能仪器教具"/>技能梳理[email protected]基于51系列单片机的智能仪器教具

无心剑中译狄金森《灵魂择其伴侣》

The performance of arm's new CPU has been improved by 22%, up to 12 cores can be combined, and the GPU is first equipped with hardware optical tracking. Netizen: the gap with apple is growing
随机推荐
Compare the maximum computing power of the Cenozoic top ant s19xp and the existing s19pro in bitland
那个程序员,被打了。
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
Pandora IOT development board learning (HAL Library) - Experiment 1 running lantern (RGB) experiment (learning notes)
Input a decimal data, convert to 8, using the sequence stack
Skill combing [email protected] somatosensory manipulator
Agile Development: super easy to use bucket estimation system
Leetcode question brushing (III) -- binary search (go Implementation)
Criu enables hot migration
Leetcode question brushing (I) -- double pointer (go Implementation)
苹果高管公然“开怼”:三星抄袭 iPhone,只加了个大屏
透過華為軍團看科技之變(五):智慧園區
Node environment configuration
腾讯云数据库工程师能力认证重磅推出,各界共话人才培养难题
ArcGIS Pro scripting tool (5) - delete duplicates after sorting
scratch绘制正方形 电子学会图形化编程scratch等级考试二级真题和答案解析2022年6月
CVPR 2022 | Tsinghua & bytek & JD put forward BRT: Bridging Transformer for vision and point cloud 3D target detection
Oracle creates a stored procedure successfully, but the compilation fails
The rising star of Goldshell STC box
"Kunming City coffee map" was opened again, and coffee brought the city closer