MYSQL 事务处理主要有两种方法
1、用 begin, rollback, commit来实现
begin或/start transaction)开始一个事务
rollback事务回滚
commit事务确认
2、直接用 SET 来改变 MySQL 的自动提交模式:
set autocommit=0禁止自动提交
set autocommit=1 开启自动提交
重要说明:
1.不管autocommit是1还是0
start transaction后,只有当commit数据才会生效,rollback后就会回滚。
2、当autocommit为 0 时
不管有没有 start transaction.
只有当commit数据才会生效,rollback后就会回滚。
3、如果autocommit为1 ,并且没有 start transaction.
调用rollback是没有用的。因为事务已经自动提交了。
事务测试1
mysql> select * from a; #有一个InnoDB 引擎的表a
+----+-----------------+-----------------------------+
| id | name | a_tx |
+----+-----------------+-----------------------------+
| 1 | 菜菜最厉害 | NULL |
| 2 | 小明 | 我爱中国 |
| 3 | 小王 | 我喜欢星星 |
| 4 | 小王 | 银河以内,千千最帅 |
+----+-----------------+-----------------------------+
4 rows in set (0.00 sec)
mysql> show create table a; #查看建表语句
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| a | CREATE TABLE `a` (
`id` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`a_tx` text,
KEY `aname_index` (`name`),
KEY `more_index` (`id`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> begin; #开启一个事务
Query OK, 0 rows affected (0.00 sec)
mysql> insert into a values(5,'小测','测试事务');#插入一条数据
Query OK, 1 row affected (0.00 sec)
mysql> select * from a; #查看表a数据
+----+-----------------+-----------------------------+
| id | name | a_tx |
+----+-----------------+-----------------------------+
| 1 | 菜菜最厉害 | NULL |
| 2 | 小明 | 我爱中国 |
| 3 | 小王 | 我喜欢星星 |
| 4 | 小王 | 银河以内,千千最帅 |
| 5 | 小测 | 测试事务 |
+----+-----------------+-----------------------------+
5 rows in set (0.00 sec)
mysql> rollback; #突然我不想要那条插入的数据了,我要撤销。
Query OK, 0 rows affected (0.01 sec)
mysql> select * from a;#撤销了插入的语句
+----+-----------------+-----------------------------+
| id | name | a_tx |
+----+-----------------+-----------------------------+
| 1 | 菜菜最厉害 | NULL |
| 2 | 小明 | 我爱中国 |
| 3 | 小王 | 我喜欢星星 |
| 4 | 小王 | 银河以内,千千最帅 |
+----+-----------------+-----------------------------+
4 rows in set (0.00 sec)
事务测试2
mysql> set autocommit = 0; #禁止自动提交
Query OK, 0 rows affected (0.00 sec)
mysql> update a set name='小测' where id = 2;#修改id=2的数据
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from a;
+----+-----------------+-----------------------------+
| id | name | a_tx |
+----+-----------------+-----------------------------+
| 1 | 菜菜最厉害 | NULL |
| 2 | 小测 | 我爱中国 |
| 3 | 小王 | 我喜欢星星 |
| 4 | 小王 | 银河以内,千千最帅 |
+----+-----------------+-----------------------------+
4 rows in set (0.00 sec)
mysql> commit;#提交事务,确认保存一样
Query OK, 0 rows affected (0.00 sec)
mysql> select * from a;
+----+-----------------+-----------------------------+
| id | name | a_tx |
+----+-----------------+-----------------------------+
| 1 | 菜菜最厉害 | NULL |
| 2 | 小测 | 我爱中国 |
| 3 | 小王 | 我喜欢星星 |
| 4 | 小王 | 银河以内,千千最帅 |
+----+-----------------+-----------------------------+
4 rows in set (0.00 sec)
mysql> rollback;#提交后回滚发现并不能回到以前小测还是小测
Query OK, 0 rows affected (0.00 sec)
mysql> select * from a;
+----+-----------------+-----------------------------+
| id | name | a_tx |
+----+-----------------+-----------------------------+
| 1 | 菜菜最厉害 | NULL |
| 2 | 小测 | 我爱中国 |
| 3 | 小王 | 我喜欢星星 |
| 4 | 小王 | 银河以内,千千最帅 |
+----+-----------------+-----------------------------+
4 rows in set (0.00 sec)
mysql> update a set name = '小惠' where id = 1;#重新修改id=1的数据
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from a;
+----+--------+-----------------------------+
| id | name | a_tx |
+----+--------+-----------------------------+
| 1 | 小惠 | NULL |
| 2 | 小测 | 我爱中国 |
| 3 | 小王 | 我喜欢星星 |
| 4 | 小王 | 银河以内,千千最帅 |
+----+--------+-----------------------------+
4 rows in set (0.00 sec)
mysql> rollback;#并没有提交,我们回滚发现可以回到以前的数据
Query OK, 0 rows affected (0.00 sec)
mysql> select * from a;
+----+-----------------+-----------------------------+
| id | name | a_tx |
+----+-----------------+-----------------------------+
| 1 | 菜菜最厉害 | NULL |
| 2 | 小测 | 我爱中国 |
| 3 | 小王 | 我喜欢星星 |
| 4 | 小王 | 银河以内,千千最帅 |
+----+-----------------+-----------------------------+
4 rows in set (0.00 sec)
其他事务命令
flag相当一定义这个保存点的名字
savepoint flag:savepoint允许在事务中创建一个保存点,一个事务中可以有多个savepoint ;
release savepoint flag:删除一个事务的保存点,当没有指定的保存点时,执行该语句会抛出一个异常;
rollback to flag:把事务回滚到标记点;
set transaction:用来设置事务的隔离级别。InnoDB存储引擎提供事务的隔离级别有
READ UNCOMMITTED、READ COMMITTED、REPEATABLE READ和SERIALIZABLE
查看默认事务隔离级别(mysql8.0)
select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ |
+-------------------------+
1 row in set (0.00 sec)
检查会话中的事务隔离级别
SELECT @@SESSION.transaction_isolation, @@SESSION.transaction_read_only;
mysql> SELECT @@SESSION.transaction_isolation, @@SESSION.transaction_read_only;
+---------------------------------+---------------------------------+
| @@SESSION.transaction_isolation | @@SESSION.transaction_read_only |
+---------------------------------+---------------------------------+
| REPEATABLE-READ | 0 |
+---------------------------------+---------------------------------+
1 row in set (0.00 sec)