当前位置:网站首页>Review of MySQL (VIII): Transactions
Review of MySQL (VIII): Transactions
2022-06-12 18:20:00 【BKSW.】
MySql Review ( 8、 ... and ): Business
Transactions can guarantee the atomicity of multiple operations , All or nothing , All or nothing .
Some characteristics of things
Four characteristics
Atomicity (Atomicity):
All operations in the whole transaction , Must be completed as a unit ( Or cancel all )
Uniformity (Consistency)
Before and after the transaction starts , The databases remain consistent
Isolation, (Isolation)
One transaction will not affect the operation of other transactions
persistence (Durability)
l After the transaction is completed , The changes made to the database are persisted in the database , It will not be rolled back .
Some of the concepts
When executed DML The statement is actually to start a transaction
You should pay attention to the rollback of transactions : Can only be rolled back insert、delete and update sentence , Cannot be rolled back select( Roll back select It doesn't make any sense ), about create、drop、alter These cannot be rolled back .
Business is only about DML Effective .
Be careful :rollback, perhaps commit Then the business is over .
Transaction commit and rollback Demo
mysql Transactions are automatically committed by default .
Just execute any one DML Statement is submitted once . The default commit is closed ---->start transaction;
- Create table
create table user(
id int (11) primary key not null auto_increment ,
username varchar(30),
password varchar(30)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

- Query table data
select * from user;

- Open transaction
start transaction;

- insert data
insert into user (username,password) values ('zhangsan','123');

- View the data
select * from user;

- Modifying data
update user set username = 'lisa' where id = 1;

- View the data
select * from user;

- Roll back the transaction
rollback;

- View the data

Auto submit mode
Auto commit moss decides how and when new transactions are started .
Start auto submit mode
– If Auto submit mode is enabled , Then single DML Statement will start a new transaction by default .
– If the statement is executed successfully , Transaction will be submitted automatically , And permanently save the execution result of the statement .
– If the statement fails to execute , Transaction will be rolled back automatically , And cancel the result of the statement .
– In auto submit mode , Can still be used START TRANSACTION Statement to explicitly start a transaction . At this time , A transaction can still contain multiple statements , Until these statements are uniformly committed or rolled back .
Disable auto submit mode :
– If Auto submit is disabled , Transactions can span multiple statements .
– under these circumstances , Transactions can be done with COMMIT and ROLLBACK Statement to explicitly commit or rollback .
Automatic submission mode can be through server variables AUTOCOMMIT To control .
-- for example :
mysql> SET AUTOCOMMIT = OFF;
mysql> SET AUTOCOMMIT = ON;
-- or
mysql> SET SESSION AUTOCOMMIT = OFF;
mysql> SET SESSION AUTOCOMMIT = ON;
show variables like '%auto%'; -- View variable status
The isolation level of the transaction
Four isolation levels
There is an isolation level for the contiguity of transactions , Theoretically, isolation levels include 4 individual :
First level : Read uncommitted (read uncommitted)
The other party's transaction has not been committed , Our current transaction can read uncommitted data from the other party .
Read uncommitted, dirty read exists (Dirty Read) The phenomenon : Indicates that dirty data has been read .
Read submitted (read committed)
We can read the data after the other party's transaction is committed .
The problem with reading submitted is : It can't be read repeatedly .Repeatable (repeatable read)
This level of isolation solves : Unrepeatable read problem .
The problem with this level of isolation is : The data read is an illusion .Serialization read / Serial reading
All problems solved .
Low efficiency , Transaction queuing is required .Oracle The default isolation level of the database is : The second level , Read submitted . mysql The default isolation level of the database is : The third level , Repeatable .
Issues of isolation level and consistency

Demonstrate two transactions
If the isolation level :
Demonstrate the first level : Read uncommitted
set global transaction isolation level read uncommitted;
Demonstrate Level 2 ; Read submitted
set global transaction isolation level read committed;
Demonstrate Level 3 : Repeatable
set global transaction isolation level repeatable read;
边栏推荐
- Introduction to reinforcement learning and analysis of classic items 1.3
- VirtualLab basic experiment tutorial -6 Blazed grating
- Typescript common types (I)
- leetcode151 翻转字符串里的单词
- PHP:Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocat
- 低代码平台调研结果
- Typescript type declaration file (III)
- leetcode 647. Palindrome substring
- Gospel of audio and video developers, rapid integration of AI dubbing capability
- VirtualLab基礎實驗教程-4.單縫衍射
猜你喜欢

Still using Microsoft office, 3 fairy software, are you sure you don't want to try?

机器学习系列(5):朴素贝叶斯

Click the list page of vant3+ts+pinia tab to enter the details. The tab on the details page is highlighted in the original position, and the refresh highlight is in the first item by default

Review of MySQL (4): sorting operation

js求斐波那契数列

Typescript type declaration file (III)

VirtualLab基础实验教程-4.单缝衍射

Gospel of audio and video developers, rapid integration of AI dubbing capability

Use applet to quickly generate app in seven steps

Introduction to reinforcement learning and analysis of classic items 1.3
随机推荐
torch. New usage of where (old but ignored usage)
leetcode151 翻转字符串里的单词
Lambda - 1
C brief introduction
1.5 what is an architect (serialization)
01 complexity
关于数据集
Strings in JS (including leetcode examples) < continuous update ~>
First principles of enterprise architecture
PHP implementation of infinite classification tree (recursion and Optimization)
A method of quickly reusing wechat login authorization in app
Can tonghuashun open an account? Can tonghuashun directly open the security of securities companies on the app
VirtualLab basic experiment tutorial -6 Blazed grating
Window版本pytorch入门深度学习环境安装与配置
LCD parameter interpretation and calculation
MYSQL:Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column
TypeScript类型声明文件(三)
VirtualLab基础实验教程-5.泊松亮斑
Virtual Lab Basic Experiment tutoriel - 4. Diffraction à fente unique
General differences between SQL server versions released by Microsoft in different periods so far, for reference