Transaction in MySQL database-detailed explanation (1)

Transaction is the smallest unit of a logical operation. Specifically, it refers to a set of SQL statements in the database. This set of SQL must be executed successfully before the data of the operation can be committed. Once there is any If one fails, it must be rolled back, and all sql operations lose their effect.

In MySQL, the sql statement of the operation is automatically submitted by default. At the same time, it also provides begin and start transaction methods to open the transaction, realize manual commit (commit), and only uncommitted operations can be rolled back (rollback). Next, let's introduce their specific usage and operation "pits" that need to be paid attention to.
1. The
autocommit parameter autocommit determines whether MySQL can submit the result directly after executing a piece of data. The default in MySQL is 1, which means automatic submission is turned on. When its value is set to 0, it will not be submitted automatically. After executing the SQL, you can manually submit or choose to roll back.
(1) View the value of autocommit, enter the MySQL database, and use

SELECT @@autocommit;

To see the value of related @autocommit

mysql> select @@autocommit;
+--------------+
| @@autocommit |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)

As you can see here, the value = 1, which means that auto-commit is turned on by default, but to implement transactions, we need to turn off auto-commit.
(2) Set the value of autocommit to turn off automatic submission. use

 set autocommit=0;

Set its value to 0 to turn off automatic submission.

mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@autocommit;
+--------------+
| @@autocommit |
+--------------+
|            0 |
+--------------+
1 row in set (0.00 sec)

In this way, after we execute the sql statement, we need to submit it manually, otherwise the result will not be modified.
(3) The effect of commit and rollback

+-------+------+-------+
| name  | age  | id    |
+-------+------+-------+
| curry |   15 | 10001 |
+-------+------+-------+

This is a user table with name, age, and id fields. First add a data to this table

mysql> insert into user values('tom',10,2);
Query OK, 1 row affected (0.01 sec)

Here we see that it has been added successfully, but is the data really saved? select and have a look

mysql> select * from user;
+-------+------+-------+
| name  | age  | id    |
+-------+------+-------+
| tom   |   10 |     2 |
| curry |   15 | 10001 |
+-------+------+-------+
3 rows in set (0.00 sec)

I found that it is indeed in the table, but there is no submission here. What happens if I roll it back?

mysql> rollback;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from user;
+-------+------+-------+
| name  | age  | id    |
+-------+------+-------+
| curry |   15 | 10001 |
+-------+------+-------+
2 rows in set (0.00 sec)

The tom information is gone. This is a way to turn off automatic commit by setting autocommit=0, and then implement transactions through rollback. But there is a problem. After setting this parameter, the entire database needs to be submitted manually when submitting operations, which is very troublesome. MySQL very intimately provides the begin and start transaction methods, which really realizes the operation of a set of data, and the manual rollback of errors!
Two, begin
before you want to write the sql statement, use it first

mysql> begin;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into user values('tom',10,2);
Query OK, 1 row affected (0.01 sec)
mysql> select * from user;
+-------+------+-------+
| name  | age  | id    |
+-------+------+-------+
| tom   |   10 |     2 |
| curry |   15 | 10001 |
+-------+------+-------+
3 rows in set (0.00 sec)

Inserted, can it be rolled back using rollback?

mysql> rollback;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from user;
+-------+------+-------+
| name  | age  | id    |
+-------+------+-------+
| curry |   15 | 10001 |
+-------+------+-------+

For a successful rollback, the following start transaction method is the same as this usage step.
Three, start transaction
is here, we will not directly roll back, after we finish operating the sql, manually submit, and then roll back to see the effect!
Preferred to use start transaction

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

Insert data

mysql> insert into user values('tom',10,2);
Query OK, 1 row affected (0.00 sec)
mysql> select * from user;
+-------+------+-------+
| name  | age  | id    |
+-------+------+-------+
| tom   |   10 |     2 |
| curry |   15 | 10001 |
+-------+------+-------+
3 rows in set (0.00 sec)

Manual submission

mysql> commit;
Query OK, 0 rows affected (0.01 sec)

Then roll back and view the results

mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from user;
+-------+------+-------+
| name  | age  | id    |
+-------+------+-------+
| tom   |   10 |     2 |
| curry |   15 | 10001 |
+-------+------+-------+
3 rows in set (0.00 sec)

It can be seen that once submitted, it cannot be rolled back. Before submitting, you can use the rollback operation to withdraw without retaining the sql effect! The next section introduces the four major characteristics of transactions in detail, the isolation level of transactions, and the problems that may arise.
Next section: Transaction characteristics and transaction isolation level issues in MySQL database

Guess you like

Origin blog.csdn.net/Wangdiankun/article/details/105868933