mysql begin commit_MySql之commit、rollback等事务控制命令

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值