当前位置:网站首页>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]基于51系列单片机的智能仪器教具
- The rising star of Goldshell STC box
- My in-depth remote office experience | community essay solicitation
- Dyson design award, changing the world with sustainable design
- 智能DNA分子纳米机器人模型来了
- Questions about cookies and sessions
- Oracle creates a stored procedure successfully, but the compilation fails
- ArcGIS PRO + PS vectorized land use planning map
- 59 websites programmers need to know
- MATLAB image histogram equalization, namely spatial filtering
猜你喜欢
机器学习面试准备(一)KNN
What is the real performance of CK5, the king machine of CKB?
安徽《合肥市装配式建筑施工图审查设计深度要求》印发;河北衡水市调整装配式建筑预售许可标准
WGet -- 404 not found due to spaces in URL
Dyson design award, changing the world with sustainable design
Go -- maximum heap and minimum heap
[email protected] control a dog's running on OLED"/>
Skill combing [email protected] control a dog's running on OLED
腾讯云数据库工程师能力认证重磅推出,各界共话人才培养难题
[email protected]+adxl345+电机震动+串口输出"/>
技能梳理[email protected]+adxl345+电机震动+串口输出
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
随机推荐
Android 开发面试真题进阶版(附答案解析)
js常见问题
腾讯云数据库工程师能力认证重磅推出,各界共话人才培养难题
断路器HystrixCircuitBreaker
Oracle creates a stored procedure successfully, but the compilation fails
ionic4 ion-reorder-group组件拖拽改变item顺序
Gd32 RT thread RTC driver function
Musk has more than 100 million twitter fans, but he has been lost online for a week
那个程序员,被打了。
Machine learning interview preparation (I) KNN
技能梳理[email protected]+adxl345+电机震动+串口输出
滴滴开源敏捷测试用例管理平台!
超长干货 | Kubernetes命名空间详解
Configure Yii: display MySQL extension module verification failed
mysql数据库基础:TCL事务控制语言
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é
从0使用keil5软件仿真调试GD32F305
今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
MATLAB image histogram equalization, namely spatial filtering
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)