当前位置:网站首页>MySQL-06

MySQL-06

2022-06-26 05:58:00 Mr.Rop

6、 Business

6.1、 What is business

Or both , Or they all failed

One by one

1、SQL perform A to B Transfer accounts A 1000 —>200 B 200

2、SQL perform B received A The money A 800 —> B 400

One by one

A set of SQL Put it in a batch to execute

Business principles :ACID principle Atomicity , Uniformity , Isolation, , persistence ( Dirty reading , Fantasy reading …)

Atomicity (Atomicity)

Either they all succeed , Or they all failed

Uniformity (Consistency)

The data integrity before and after the transaction should be consistent

persistence (Durability) – Transaction submission

Once the transaction is committed, it is irreversible , Be persisted to the database

Isolation, (Isolation)

Transaction isolation is when multiple users access the database concurrently , A transaction opened by a database for each user , Can't be disturbed by the operation data of other transactions , Multiple concurrent transactions should be isolated from each other .

Some of the problems caused by isolation

Dirty reading :

A transaction reads uncommitted data from another transaction .

It can't be read repeatedly :

Read a row of data in a table within a transaction , Multiple reads result is different .( This is not necessarily a mistake , It's just that on some occasions it's not right )

Virtual reading ( Fantasy reading ):

It refers to the data inserted by other transactions in a transaction , Leading to inconsistent reading before and after .
( It's usually the line effect , One more line )

Perform transactions

-- ==========  Business  =============
-- mysql  The default transaction is auto commit 
SET autocommit =0 /*  close  */
SET autocommit =1 /*  Turn on ( default ) */

--  Handle transactions manually 
SET autocommit =0 --  Turn off auto submit 


--  The transaction open 
START TRANSACTION --  Mark the beginning of a transaction , After this SQL All in the same transaction 


--  Submit : Persistence ( success !)
COMMIT
--  Roll back : Return to the original ( Failure !)
ROLLBACK


--  End of transaction 
SET autocommit =1 --  Turn on auto submit 

--  understand 
SAVEPOINT  Save roll call  --  Set a save point for a transaction 
ROLLBACK TO SAVEPOINT  Save roll call  --  Roll back to savepoint 
RELEASE SAVEPOINT  Save roll call  --  Undo savepoint 

Simulation scenario

-- ==========  Business  =============
--  Analog transfer 
--  Create database 
CREATE DATABASE shop CHARACTER SET utf8 COLLATE utf8_general_ci

USE shop

--  Create table 
CREATE TABLE `account`(
  `id` INT(3) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(30) NOT NULL,
  `money` DECIMAL(9,2) NOT NULL,
  PRIMARY KEY (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8

--  Add data 
INSERT INTO account(`name`,`money`)
VALUES('A',2000.00),('B',10000.00)

--  Analog transfer , Business 
SET autocommit =0 --  Turn off auto submit 
START TRANSACTION --  Start a transaction ( A set of transactions )

UPDATE account SET money=money-500 WHERE `name`='A' -- A reduce 500
UPDATE account SET money=money+500 WHERE `name`='B' -- B Add 500

COMMIT; --  Commit transaction 
ROLLBACK; --  Roll back 

SET autocommit = 1; --  Restore defaults 
原网站

版权声明
本文为[Mr.Rop]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180503012805.html