The latest super-detailed MySQL in 2022 (very suitable for beginners, basics)

  Personal Homepage: Personal Homepage
​ Series Columns: MySQL Database

Reminder: This blog is the entire content of the basic article, collect it in case you can't find it.

previous section


 

1. Mysql download and installation start (suitable for small partners who have not downloaded and followed good MySQL, super detailed graphic tutorial)

2. Graphical interface tool (more convenient to operate the database, super detailed graphic installation tutorial)

3. SQL (the core of MySQL)

 1. DDL (database operations, table operations, table operations - data types, table operations - cases)

 2. DML (add data, modify data, delete data)

3. DQL (basic syntax, basic query, conditional query, aggregate function, grouping query, sorting query, paging query, case, execution order)

 4. DCL (admin user, permission control)

4. Functions (string functions, numerical functions, date functions, process functions)

5. Constraints (primary key, foreign key, non-null, unique, default)

Six. Multi-table query (multi-table relationship, inner join, outer join, self join, sub query)


Very useful database tool: (highly recommended)

Encyclopedia of Programming Utilities (1) (Database Encyclopedia)

Programming Utility Tool Encyclopedia (2) (Online Simulation Data Generator)

Click to see it now.


Contents of this section

1. Business Introduction

2. Transaction Operations 

1. Uncontrolled transactions

1). Test normal conditions

2). Test for exceptions  

2. Control transaction one

3. Control transaction 2

3. Four characteristics of transaction

4. Concurrent transaction issues

5. Transaction isolation level


1. Business Introduction

A transaction is a collection of operations, which is an indivisible unit of work. A transaction submits or revokes operation requests to the system as a whole, that is, these operations either succeed or fail at the same time.

For example : Zhang San transfers 1,000 yuan to Li Si, the money in Zhang San's bank account is reduced by 1,000 , and the money in Li Si's bank account is increased by 1,000. This set of operations must be within the scope of a transaction, either all succeed or all fail

 Normal situation: The transfer operation needs to be completed in the following three steps. After the three steps are completed , Zhang San decreases by 1000, while Li Si increases by 1000, and the transfer is successful :

 Abnormal situation: The transfer operation is also divided into the following three steps to complete . In the third step, an error is reported , which causes Zhang San to reduce 1,000 yuan , while Li Si's amount does not change , which causes data corruption. If it doesn't match , there is a problem.

 In order to solve the above problems, we need to complete the transaction through the data. We only need to open the transaction before the execution of the business logic, and submit the transaction after the execution. If an error is reported during execution, the transaction is rolled back and the data is restored to the state before the transaction started.

 Note: The default MySQL transaction is automatically committed, that is, when a DML statement is executed, MySQL will immediately and implicitly commit the transaction

2. Transaction Operations 

data preparation:
drop table if exists account;
create table account
(
    id    int primary key AUTO_INCREMENT comment 'ID',
    name  varchar(10) comment '姓名',
    money double(10, 2) comment '余额'
) comment '账户表';
insert into account(name, money)
VALUES ('张三', 2000),
       ('李四', 2000);

1. Uncontrolled transactions

1). Test the normal situation

-- 1. 查询张三余额
select * from account where name = '张三';
-- 2. 张三的余额减少1000
update account set money = money - 1000 where name = '张三';
-- 3. 李四的余额增加1000
update account set money = money + 1000 where name = '李四';
select * from account;

 After the test is completed, check the status of the data, and you can see that the data operation is consistent before and after.

2). Test exceptions 

-- 1. 查询张三余额
select * from account where name = '张三';
-- 2. 张三的余额减少1000
update account set money = money - 1000 where name = '张三';
出错了....-- 故意加上的
-- 3. 李四的余额增加1000
update account set money = money + 1000 where name = '李四';

 

We restore the data to 2000 , and then execute the above SQL statement once again ( error .... This sentence does not conform to the SQL syntax, the execution will report an error ) , check the final data situation , and find that the data is inconsistent before and after the operation .
Reason: Zhang San minus 1000 before the error was executed, but Li Si plus 1000 was not executed

 

 

2. Control transaction one

1). View / set transaction submission method

SELECT @@autocommit ; SET @@autocommit = 0 ;
2). Commit the transaction
COMMIT;
3). Roll back the transaction
ROLLBACK;
Note: In the above method, we have modified the automatic commit behavior of the transaction , and changed the default automatic commit to manual commit. At this time, none of the DML statements we execute will be committed , and we need to manually execute commit to commit.

 

3. Control transaction 2

1) .Open the transaction

START TRANSACTION 或 BEGIN ;

2). Commit the transaction

COMMIT;
3). Roll back the transaction
ROLLBACK;
Transfer case:
-- 开启事务
start transaction
-- 1. 查询张三余额
select * from account where name = '张三';
-- 2. 张三的余额减少1000
update account set money = money - 1000 where name = '张三';
-- 3. 李四的余额增加1000
update account set money = money + 1000 where name = '李四';
-- 如果正常执行完毕, 则提交事务
 commit;
-- 如果执行过程中报错, 则回滚事务
-- rollback;

3. Four characteristics of transaction

  • Atomicity : A transaction is an indivisible smallest unit of operation that either all succeeds or all fails.
  • Consistency : When a transaction completes, all data must be in a consistent state.
  • Isolation : The isolation mechanism provided by the database system ensures that transactions run in an independent environment thatis not affected by external concurrent operations
  • Durability : Once a transaction is committed or rolled back, its changes to the data in the database are permanent.
The above are the four characteristics of transactions, referred to as ACID .

 

4. Concurrent transaction issues

1). Dirty read: A transaction reads data that has not yet been committed by another transaction.

For example , B reads the data that A has not submitted. 

 

2). Non-repeatable read: A transaction reads the same record successively, but the data read twice is different, which is called non-repeatable read.

Transaction A reads the same record twice, but the data read is different 

3). Phantom reading: When a transaction queries data according to conditions, there is no corresponding data row, but when inserting data, it is found that this row of data already exists, as if there is a " phantom " .

 

 

5. Transaction isolation level

In order to solve the problems caused by concurrent transactions, the transaction isolation level was introduced in the database. There are mainly the following:

 1). View transaction isolation level

SELECT @@TRANSACTION_ISOLATION;
2). Set transaction isolation level
SET [ SESSION | GLOBAL ] TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE }
Note: The higher the transaction isolation level, the more secure the data, but the lower the performance.

Guess you like

Origin blog.csdn.net/Javascript_tsj/article/details/124316478