A transaction is a logical set of actions that make up each unit of this group of operations, or all of them succeed or all fail, a feature that is a transaction
Note: MySQL data support transactions, but requirements must be InnoDB storage engine
Solve this problem:
MySQL transactions solve this problem, because the MySQL transaction characteristics, requires this group of operations, either all successful, or all failed, so that an operation to avoid a successful operation failed. Facilitate data security
How to use:
(1) Before executing the SQL statement, we need to open the transaction start transaction;
(2) Normal execution of our SQL statements
(3) When the SQL statement is executed, there are two situations:
1, all successful, we want to commit the SQL statement to the database impact to the database, Committ
2, some SQL statements fail, we perform rollback (rollback), and the database operation will be undone quickly
(Note: MySQL Data support transactions, but the requirements must be InnoDB storage engine)
Mysql> CREATE table Bank (name varchar, money decimal (5,1)) Engine=innodb Defau
Lt Charset=utf8;
Mysql> inset into bank values (' Shaotuo ', 1000), (' Laohu ', 5000);
The------did not succeed in rolling back the execution rollback
mysql> start transaction; Open transaction
Query OK, 0 rows Affected (0.00 sec)
Mysql> Update Bank set money=money+500 where name= ' Shaotuo ';
Query OK, 1 row Affected (0.00 sec)
Rows matched:1 changed:1 warnings:0
Mysql> Update Bank set moey=money-500 where name= ' Laohu ';
ERROR 1054 (42S22): Unknown column ' Moey ' in ' Field list '
mysql> rollback; As long as there is one unsuccessful, perform the rollback operation
Query OK, 0 rows affected (0.01 sec)
Mysql> Update Bank set money=money+500 where name= ' Shaotuo ';
Query OK, 1 row affected (0.01 sec)
Rows matched:1 changed:1 warnings:0
Mysql> Update Bank set money=money-500 where name= ' Laohu ';
Query OK, 1 row Affected (0.00 sec)
Rows matched:1 changed:1 warnings:0
Mysql> commit; Commit two after successful execution (as long as the Commit,sql statement does not affect the real database)
Query OK, 0 rows affected (0.05 sec)
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.