当前位置:网站首页>8、 Transaction control language of MySQL
8、 Transaction control language of MySQL
2022-07-03 10:34:00 【sherry 96】
List of articles
Preface
This section introduces MySQL Medium TCL( Transaction control language ), The content mainly includes the concept of transaction 、 characteristic 、 Isolation level of database, etc .
🧑 Transaction control language
Business : One or a group of sql Statements form an execution unit , This execution unit is either all executed , Or all Don't execute .
Case study : Transfer accounts
characteristic
The interview is easy to test
- The transaction ACID(acid) attribute
① Atomicity
(Atomicity): Atomicity means that a transaction is an indivisible unit of work , Either the operations in the transaction occur , Or none at all .② Uniformity
(Consistency): Transactions must transform the database from one consistency state to another .③ Isolation,
(Isolation): Transaction isolation means that the execution of one transaction cannot be interfered by other transactions , That is, the operations and data used within a transaction are isolated from other concurrent transactions , Transactions that execute concurrently cannot interfere with each other .④ 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
MySQL Storage engine in :
1、 Concept : stay mysql The data in is stored in files using various techniques ( Or memory ) in .
2、 adopt show engines; Check it out. mysql Supported storage engines .
3、 stay mysql The most used storage engines in are :innodb, myisam,memory etc. . among inndb Support transactions , and myisam、memory Transaction is not supported
The creation of transactions
Implicit transaction : The transaction has no obvious opening and closing marks
such as insert、update、delete sentence
Explicit transaction : A transaction has distinct open and end tags
Premise : Auto submit must be disabled first
Show transaction writing steps :
step 1: Open transaction
set autocommit=0;
start transaction; Optional
step 2: Write... In a transaction sql sentence (select insert update delete)
sentence 1;
sentence 2;
…
step 3: End the business
commit; Commit transaction
rollback; Roll back the transaction
savepoint The node name ; Set the savepoint ;
- Demonstrate the transaction for delete and truncate Differences in processing
SET autocommit=0;
START TRANSACTION;
DELETE FROM account;
ROLLBACK;
- demonstration savapoint Use
SET autocommit=0;hui
START TRANSACTION;
DELETE FROM account WHERE id=25;
SAVAPOINT a; # Set the savepoint
DELETE FROM account WHERE id=28;
ROLLBACK TO a; # Roll back to savepoint
Isolation level of database
- For multiple transactions running simultaneously , When these transactions access
The same data in the database
when , If necessary isolation mechanism is not adopted , It can lead to various concurrency problems :
①
Dirty reading
: For two things T1, T2, T1 Read has been T2 Fields updated but not yet submitted . after , if T2 Roll back ,T1 The read content is temporary and invalid .
②It can't be read repeatedly
: For two things T1,T2,T1 Read a field , then T2 Updated fields . after ,T1 Read the same field again , The value is different .
③Fantasy reading
: For two things T1,T2,T1 Read a field from a table , then T2 Some new rows have been inserted into the table . after , If T1 Read the same table again , There will be more lines .
- Isolation of database transactions : The database system must have the ability to isolate and run transactions concurrently , So that they don't interact with each other , Avoid all kinds of concurrent problems .
The degree to which a transaction is isolated from other transactions is called the isolation level
. The database specifies multiple transaction isolation levels , Different isolation levels correspond to different interference levels , Higher isolation level , The better the data consistency , But the less concurrent .- Database provides 4 Transaction isolation level :
Isolation level | describe |
---|---|
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 |
READ COMMITED( Read submitted data ) | Only transactions are allowed to read changes that have been committed by other transactions . Avoid dirty reading , But the problems of non repeatable reading and unreal reading may still occur |
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 repeatable reading , But the problem of unreal reading still exists . |
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 Supported by 2 Transaction isolation level :
READ COMMITED
, SERIALIZABLE.Oracle The default transaction isolation level is :READ COMMITED - Mysql Support 4 Transaction isolation level .Mysql The default transaction isolation level is :
REPEATABLE READ
- Every time you start one mysql Program , You get a separate database connection . Every database connection has a global variable @@tx_isolation, Represents the current transaction isolation level .
- View the current isolation level :[email protected]@tx_isolation;
- Set up current mySQL Isolation level of the connection :
set transaction isolation levelread committed
; - Set the global isolation level of the database system :
setglobal
transaction isolation levelread committed
;
Summary of isolation levels of transactions
Dirty reading | It can't be read repeatedly | Fantasy reading | |
---|---|---|---|
read uncommitted | √ | √ | √ |
read committed | × | √ | √ |
repeatable read | × | × | √ |
serializable | × | × | × |
mysql The default The third isolation level repeatable read
oracle The default second isolation level in read committed
- Check the isolation level
select @@tx_isolation; - Set isolation level
set session | global transaction isolation level Isolation level
summary
This section mainly introduces MySQL Transaction control language in , thus ,MySQL The language in has been basically introduced , Welcome friends to continue to pay attention and learn !
边栏推荐
- A super cool background permission management system
- What useful materials have I learned from when installing QT
- Inverse code of string (Jilin University postgraduate entrance examination question)
- 深度学习入门之自动求导(Pytorch)
- 波士顿房价预测(TensorFlow2.9实践)
- 实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
- [graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
- 六、MySQL之数据定义语言(一)
- 20220608 other: evaluation of inverse Polish expression
- Configure opencv in QT Creator
猜你喜欢
Ut2012 learning notes
Adaptive Propagation Graph Convolutional Network
七、MySQL之数据定义语言(二)
Tensorflow - tensorflow Foundation
Preliminary knowledge of Neural Network Introduction (pytorch)
3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
Timo background management system
Stroke prediction: Bayesian
Handwritten digit recognition: CNN alexnet
MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
随机推荐
安装yolov3(Anaconda)
Data classification: support vector machine
High imitation Netease cloud music
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
Ind FXL first week
深度学习入门之自动求导(Pytorch)
『快速入门electron』之实现窗口拖拽
A super cool background permission management system
Leetcode刷题---1
Boston house price forecast (tensorflow2.9 practice)
ECMAScript -- "ES6 syntax specification # Day1
[LZY learning notes -dive into deep learning] math preparation 2.5-2.7
Label Semantic Aware Pre-training for Few-shot Text Classification
Leetcode刷题---704
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
[graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
Advantageous distinctive domain adaptation reading notes (detailed)
Ut2012 learning notes
Seata分布式事务失效,不生效(事务不回滚)的常见场景
二分查找法