What is the transaction in Mysql? how to use bitsCN.com?
What is a transaction?
A transaction is a logical group of operations that constitute each unit of this group of operations. if it fails or fails, it is a transaction.
Note: mysql data supports transactions, but must be an innoDB storage engine.
Solve this problem:
Mysql transactions solve this problem. because of the transaction characteristics of mysql, this set of operations is required, either completely successful or all failed, which avoids the success of an operation. Conducive to data security
How to use:
(1) before executing an SQL statement, start transaction is enabled for the transaction;
(2) normal execution of our SQL statements
(3) when the SQL statement is executed, there are two situations:
1. all are successful. we need to submit the impact of SQL statements on the database to the database, committ
2. if some SQL statements fail, we execute rollback to quickly cancel database operations.
(Note: mysql data supports transactions, but must be an innoDB storage engine)
Mysql> create table bank (name varchar (20), money decimal (5, 1) engine = innodb defau
Lt charset = utf8;
Mysql> inset into bank values ('shaotuo ', 1000), ('laohu', 5000 );
------ Failed to roll back and execute rollback
Mysql> start transaction; // start the 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 'moine' in 'Field list'
Mysql> rollback; // if one fails, perform the rollback operation.
Query OK, 0 rows affected (0.01 sec)
Mysql> select * from bank;
+ --------- + -------- +
| Name | money |
+ --------- + -------- +
| Shaotuo | 1000.0 |
| Laohu | 5000.0 |
+ --------- + -------- +
------ Perform the commit operation after the operation is successful
Mysql> start transaction; // start the 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.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; // execute commit after both of them are successful (as long as no commit is executed, the SQL statement will 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.