A transaction in MySQL

Source: Internet
Author: User

One: What is a transaction
Bank Transfer! Zhang San 10000 to John Doe's account, which actually requires two SQL statements:
To Zhang San's account minus 10000 yuan;
Add 10000 yuan to John Doe's account.
If the first SQL statement succeeds, the program is interrupted before the second SQL statement is executed (possibly throwing an exception, or it may
What other reason), then John Doe's account did not add 10000 yuan, but Zhang San minus 10000 yuan. This is definitely not going to work!
You probably already know what a business is now! Multiple operations in a transaction are either completely successful or fail completely! There can't be half a success.
The situation! That is, to Zhang San account minus 10000 Yuan if successful, then to John Doe's account plus 10000 Yuan operation must also be successful
Otherwise, to Zhang San minus 10000 yuan, and to John Doe plus 10000 Yuan is a failure!


Two: Four characteristics of things (ACID)
Interview!
The four main features of a transaction are:
atomicity (atomicity): All operations in a transaction are non-divisible atomic units. All operations in a transaction are executed successfully

, or all execution fails.


Consistency ( consistency): After a transaction executes, the database state is aligned with other business rules. such as transfer business, regardless of the transaction

Successful execution, the sum of the two account balances involved in the transfer should be constant.


Isolation ( isolation): isolation means that in concurrent operations, different transactions should be isolated, so that each concurrent transaction

Do not interfere with each other.


Persistence (Durability):Once the transaction commits successfully, all data operations in the transaction must be persisted to the database, even if
Once the transaction is committed, the database crashes immediately, and when the database restarts, it must be guaranteed to recover the data through some mechanism.


Three: The transaction in MySQL
By default, each SQL statement executed by MySQL is a separate transaction. If you need to include multiple SQL statements in a transaction, that
You need to open transactions and end transactions.
Open transaction: Start transaction;
End transaction: Commit or rollback.
Execute the Strat transaction before executing the SQL statement, which opens a transaction (the starting point of the transaction) and can then execute multiple
SQL statement to end the transaction, commit represents commit, that is, the impact of multiple SQL statements in the transaction is persisted to the database. Or
Rollback, which means rollback, rolls back to the start of the transaction, and all previous actions have been undone!


first create a table:
CREATE TABLE Account (
ID INT PRIMARY KEY auto_increment,
NAME VARCHAR (+),
balance NUMERIC (10.2)
);


INSERT into account ( name,balance) VALUES (' Zs ', 100000);
INSERT into account ( name,balance) VALUES (' ls ', 100000);
INSERT into account ( name,balance) VALUES (' ww ', 100000);


SELECT * from account;


use rollback to return to the beginning of things, so the statement is invalid
START TRANSACTION;
UPDATE account SET balance=balance-1000 WHERE id=1;
UPDATE account SET balance=balance+1000 WHERE id=2;
ROLLBACK;


commit means commit, and SQL statements are saved to the database after execution
START TRANSACTION;
UPDATE account SET balance=balance-1000 WHERE id=1;
UPDATE account SET balance=balance+1000 WHERE id=2;
COMMIT;


after quit, MySQL automatically rolls back things
START TRANSACTION;
UPDATE account SET balance=balance-10000 WHERE id=1;
UPDATE account SET balance=balance+10000 WHERE id=2;
quit;



Copyright Notice: Bo Master original articles, reproduced please indicate the source. Http://blog.csdn.net/dzy21

A transaction in MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.