[0928 | Day 39] transactions (succinctly)

First, the transaction

In mysql, the transaction is in fact a minimal indivisible unit of work, the transaction can ensure the integrity of a business

For example, our bank transfer:
   A to b turn 100 dollars, equivalent to
   update user set monry=money-100 where name='a'
update user set monry=money+100 where name='b'

Need two statements, in reality, if only one statement is executed successfully, and the other is not executed successfully, before and after data inconsistency occurs.

Transaction used to solve: multiple sql statements may be required while the success or failure on at the same time.

How to 1. mysql control transactions?

mysql default is automatically submitted

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

2. What is the role of the default transaction open?

  • When we went to execute a sql statement, the effect will be reflected immediately and can not be rolled back.
  • Here again there are some concepts (rollback), us, for example:
  • Create a database, and then create tables, insert data
mysql> create database bank;
Query OK, 1 row affected (0.64 sec)

mysql> use bank;
Database changed

mysql> create table user(
    -> id int primary key,
    -> name varchar(20),
    -> money int
    -> );
Query OK, 0 rows affected (0.93 sec)
mysql> insert into user values(1,'a',1000);
Query OK, 1 row affected (0.51 sec)

mysql> select * from user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |  1000 |
+----+------+-------+
1 row in set (0.00 sec)

Transaction Rollback: Undo the effect of the implementation of sql (rollback;)

The following call rollback

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

mysql> select * from user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |  1000 |
+----+------+-------+
1 row in set (0.00 sec)

Can see that even called rollback, the data still exists, indicating that it is not possible to roll back, added the statement after the implementation of the data submitted to default.

0 way we can be changed by setting the default transaction, which is submitted to false mysql default settings set autocommit=0;(1 on, 0 off)

mysql> set autocommit=0;
Query OK, 0 rows affected (0.54 sec)

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

The above operation, the closed automatically submitted mysql (the commit)

Here in operation again :( this time to verify the auto-commit has been closed)

mysql> insert into user values(2,'b',1000); #插入数据
Query OK, 1 row affected (0.00 sec)

mysql> select * from user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |  1000 |
|  2 | b    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> rollback;  #进行回滚
Query OK, 0 rows affected (0.07 sec)

mysql> select * from user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |  1000 |
+----+------+-------+
1 row in set (0.00 sec)

No surprise surprise, just insert the data is gone. When we finished the implementation of the statement (closed default submission), to see the effect is only a temporary effect, and there is no real place in our database which is at a virtual table.

Here you can use commit submit

#再插入一次数据
mysql> insert into user values(2,'b',1000);
Query OK, 1 row affected (0.00 sec)

#手动提交数据
mysql> commit;
Query OK, 0 rows affected (0.15 sec)

#再撤销,是不可以撤销的(事务的一个特性:持久性)
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |  1000 |
|  2 | b    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

As long as the commit, rollback no use, durable oh ~

Auto Commit: @@ autocommit = 1

Manual submission: commit

Rollback: rollback (in the absence of submissions, it can be submitted)

In other words, the transaction provides us with an opportunity to return! ! !

As said earlier, in the case found that a statement is not successfully executed, it can be rolled back. After the check, and then manually commit it to take effect, produce real results.

We then the transaction is closed, to automatic submission:

mysql> set autocommit=1;
Query OK, 0 rows affected (0.00 sec)

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

3. Open other matters?

Manually open transaction: begin or start transaction, can help us to manually open a transaction

Here to make transfers examples:

mysql> update user set money=money-100 where name='a';
Query OK, 1 row affected (0.95 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update user set money=money+100 where name='b';
Query OK, 1 row affected (0.14 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |   900 |
|  2 | b    |  1100 |
+----+------+-------+
2 rows in set (0.00 sec)

#事务回滚
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |   900 |
|  2 | b    |  1100 |
+----+------+-------+
2 rows in set (0.00 sec)

Here rollback is now found no effect, because now autocommit = 1, the automatic submission mode, whenever the execution sentence immediately into effect.

Now before you enter these two statements, input begin or start transaction

#加上begin;或者 start transaction 开启事务
mysql> begin;
Query OK, 0 rows affected (0.01 sec)

#对a、b进行转账操作
mysql>  update user set money=money-100 where name='a';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> update user set money=money+100 where name='b';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

#看一下表
mysql> select * from user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |   800 |
|  2 | b    |  1200 |
+----+------+-------+
2 rows in set (0.00 sec)

#事务回滚
mysql> rollback;
Query OK, 0 rows affected (0.57 sec)

mysql> select * from user;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | a    |   900 |
|  2 | b    |  1100 |
+----+------+-------+
2 rows in set (0.00 sec)

After the transaction open, once the commit submission, it can not be rolled back (that is, the transaction at the time of filing of the end), then re rollback no use

Second, the four characteristics of things

1. atomicity

  • A transaction is a set of indivisible units, at the same time either succeed or not succeed at the same time

2. Consistency

  • Data integrity should be the same thing before and after the (database integrity: If the database at a certain point in time, all the data are in line with all the constraints, called for the integrity of the database state)

3. Isolation

  • Isolation of things means that when a plurality of users concurrent access to data, a user can not be firm things interfere with other users, data between a plurality of concurrent transactions to be separated from each other

4. endurance

  • Persistence means that once a thing is submitted, it changed data is permanent, then even if the database fails nor should it have any impact

Guess you like

Origin www.cnblogs.com/fxyadela/p/11605151.html
39