mysql之commit,transaction事物控制


簡單來說,transaction就是用來恢復為以前的數據。
舉個例子,我想把今天輸入到數據庫里的數據在晚上的時候全部刪除,那么我們就可以在今天早上的時候開始transaction事物,令autocommit關閉並且執行commit,然后再開始輸入數據,到晚上的時候,可以執行rollback恢復到今天沒輸入數據的狀態,也就是恢復到commit前的數據。

[root@localhost ~]# mysql -uroot -p              #登錄數據庫
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database | +--------------------+
| information_schema | | mysql | | performance_schema |
| test | +--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> create database bp                #我自己創建一個數據庫用來做這個實驗
 -> ;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> 



MariaDB [(none)]> use bp;
Database changed
MariaDB [bp]> create table test(id int,name varchar(20));               #建表
Query OK, 0 rows affected (0.08 sec)

MariaDB [bp]> insert into test values(1,'123');                         
Query OK, 1 row affected (0.06 sec)

MariaDB [bp]> insert into test values(2,'323');
Query OK, 1 row affected (0.01 sec)

MariaDB [bp]> select * from test;
+------+------+
| id | name | +------+------+
|    1 | 123  |
| 2 | 323 | +------+------+
2 rows in set (0.00 sec)

MariaDB [bp]> show variables like '%commit%';                       #查看autocommit是否關閉,可以看到現在開啟着
+-------------------------------------------+-------+
| Variable_name | Value | +-------------------------------------------+-------+
| aria_group_commit                         | none  |
| aria_group_commit_interval | 0 | | autocommit | ON | | innodb_commit_concurrency | 0 | | innodb_flush_log_at_trx_commit            | 1     |
| innodb_use_global_flush_log_at_trx_commit | ON | +-------------------------------------------+-------+
6 rows in set (0.00 sec)

MariaDB [bp]> set autocommit=0;                             #關閉autocommit
Query OK, 0 rows affected (0.00 sec)

MariaDB [bp]> show variables like '%commit%';
+-------------------------------------------+-------+
| Variable_name | Value | +-------------------------------------------+-------+
| aria_group_commit                         | none  |
| aria_group_commit_interval | 0 | | autocommit | OFF | | innodb_commit_concurrency | 0 | | innodb_flush_log_at_trx_commit            | 1     |
| innodb_use_global_flush_log_at_trx_commit | ON | +-------------------------------------------+-------+
6 rows in set (0.00 sec)

MariaDB [bp]> start transaction;                            #開始事物
Query OK, 0 rows affected (0.00 sec)

MariaDB [bp]> delete from test where id=1;
Query OK, 1 row affected (0.00 sec)

MariaDB [bp]> select * from test;
+------+------+
| id | name | +------+------+
| 2 | 323 | +------+------+
1 row in set (0.00 sec)

MariaDB [bp]> commit;                                   #記錄前面的數據
Query OK, 0 rows affected (0.01 sec)

MariaDB [bp]> delete from test where id=2;              #刪除數據
Query OK, 1 row affected (0.00 sec)

MariaDB [bp]> select * from test;
Empty set (0.00 sec)

MariaDB [bp]> rollback;                                 #回滾到commit記錄的數據
Query OK, 0 rows affected (0.00 sec)

MariaDB [bp]> select * from test;                       #回滾成功
+------+------+
| id | name | +------+------+
| 2 | 323 | +------+------+
1 row in set (0.00 sec)

MariaDB [bp]> 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM