What is the transaction in Mysql how to use _mysql

Source: Internet
Author: User
Tags commit rollback
What is a transaction?

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);

Mysql> Select*from Bank;
+---------+--------+
| name | Money |
+---------+--------+
| Shaotuo | 1000.0 |
| Laohu | 5000.0 |
+---------+--------+

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> Select*from Bank;
+---------+--------+
| name | Money |
+---------+--------+
| Shaotuo | 1000.0 |
| Laohu | 5000.0 |
+---------+--------+
Commit after------successful
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.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)

Mysql> Select*from Bank;
+---------+--------+
| name | Money |
+---------+--------+
| Shaotuo | 1500.0 |
| Laohu | 4500.0 |
+---------+--------+
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.