Free learning recommendation: mysql video tutorial
Article Directory
1 , Introduction and use of transactions
You can view the storage engines supported by mysql through show engines;
, among which innodb
supports transactions, and myisam
, memory
, etc. do not support transactions.
Transaction: One or a group of sql statements form a sql unit. This execution unit either executes all, or does not execute all
.
Transactions have four attributesACID
Atomicity | A transaction is an indivisible Unit of Work |
---|---|
Consistency | The transaction must cause the database to change from a consistent state To another consistency state |
Isolation | The execution of a transaction cannot be interfered with by other transactions, Concurrently executed transactions cannot interfere with each other |
Durability(Durability) | Once a transaction is submitted, Changes to the data in the database are permanent |
Implicit transactions: Transactions have no obvious opening and closing marks, such as insert , delete and update statements.
Explicit transactions: Transactions have obvious opening and closing marks. The prerequisite for use is that the auto-commit function must be disabled.
After the DELETE statement is rolled back, it can still be restored; after the TRUNCATE statement is rolled back, it cannot be restored.
【演示事务的使用步骤 】DROP TABLE IF EXISTS account;CREATE TABLE account( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(20), balance DOUBLE);INSERT INTO account(username,balance)VALUES('张无忌',1000),('赵敏',1000);# 第一步:关闭自动提交SET autocommit=0;START TRANSACTION;# 可以省略# 第二步:编写一组事务UPDATE account SET balance=balance+500 WHERE username='张无忌';UPDATE account SET balance=balance-500 WHERE username='赵敏';# 第三步:结束事务#commit;# 提交ROLLBACK; # 回滚SELECT * FROM account;【演示savepoint的使用】SET autocommit=0;START TRANSACTION;DELETE FROM account WHERE id=1;SAVEPOINT a;# 设置保存点DELETE FROM account WHERE id=2;ROLLBACK TO a; # 回滚到保存点
2. Transaction concurrency issues and solutions
For multiple transactions running at the same time, when these transactions access the same data in the database, if no necessary steps are taken The isolation mechanism will lead to various concurrency problems.
Dirty read
: A transaction reads data updated by other things but does not commit
.
Non-repeatable read
: A transaction is read multiple times, and the results are different.
Phantom read
: A transaction reads data inserted by another transaction but does not commit
.
The solution to transaction concurrency problems is to avoid concurrency problems by setting the isolation level of the transaction.
Every time a mysql program is started, a separate database connection will be obtained. Each database connection has a global variable @@tx_isolation
, indicating the current transaction isolation level.
View the current isolation level: select @@tx_isolation;
Isolation level of transaction | Dirty read | Non-repeatable read | Phantom read |
---|---|---|---|
##read uncommitted read uncommitted | UnresolvedUnresolved | Unresolved | |
read committed Read committed (Oracle default) | Solved√Unresolved | Unresolved | |
Resolved√ | Resolved√ | Unresolved | |
Solution√ | Solution√ | Solution√ |
read committed; Set the global isolation level of the database system: set
global transaction isolation level read committed;
The meaning of view: a new feature that appeared after mysql5.1 version, a virtual table, row and column data come from the table used in the query of the custom view, and It is dynamically generated when using the view. It only saves the SQL logic and does not save the query results.
Application scenarios:
The same query results are used in multiple places.Creation syntax keywords | Physical space occupied | Use | |
---|---|---|---|
create view | just saves the sql logic | You can add, delete, modify and check, But generally only supports querying | |
create table | saved data | supports addition, deletion, modification and query |
The above is the detailed content of MySQL Lecture 4: TCL Transaction Control Statement. For more information, please follow other related articles on the PHP Chinese website!