DDL 事务(1) 死在mysql

引子

BEGIN;
select * from test;
delete from test where id = '1';
alter table test add column `comment` varchar(64);
delete from test where id = '2';
ROLLBACK;

想想看,最后执行结果是什么?

实操

Mysql 5.x 执行情况

在这里插入图片描述

Mysql 8.x 执行情况

在这里插入图片描述

啊,发出土拨鼠的尖叫—
为什么事务失效了?不是要回滚吗?

原因

mysql 不支持 ddl 事务。点击链接,跳转原文

Statements That Cannot Be Rolled Back
Some statements cannot be rolled back. In general, these include data definition language (DDL) statements, such as those that create or drop databases, those that create, drop, or alter tables or stored routines.
You should design your transactions not to include such statements. If you issue a statement early in a transaction that cannot be rolled back, and then another statement later fails, the full effect of the transaction cannot be rolled back in such cases by issuing a ROLLBACK statement.

某些语句无法回滚。通常,这些语句包括数据定义语言(DDL)语句,例如创建或删除数据库的语句,创建、删除或更改表或存储例程的语句。
您应该将交易设计为不包含此类报表。如果在事务的早期发出一个无法回滚的语句,然后另一个语句稍后失败,则在这种情况下,无法通过发出ROLLBACK语句来回滚事务的全部效果。

DDL statements, atomic or otherwise, implicitly end any transaction that is active in the current session, as if you had done a COMMIT before executing the statement. This means that DDL statements cannot be performed within another transaction, within transaction control statements such as START TRANSACTION … COMMIT, or combined with other statements within the same transaction.

DDL语句,无论是原子语句还是其他语句,都会隐式地结束当前会话中活动的任何事务,就好像在执行该语句之前执行了COMMIT一样。这意味着DDL语句不能在另一个事务中执行,也不能在事务控制语句(如START transaction…)中执行。。。COMMIT,或与同一事务中的其他语句组合。

ok,fine,散场吧~

附录

SQL的语句,我们通常分为

  • DQL Data Query Language 数据查询语言。
    就是我们最经常用到的 SELECT(查)语句 。主要用来对数据库中的数据进行查询操作。
  • DML Data manipulation language数据操纵语言
    就是我们最经常用到的 INSERT(增)、DELETE(删)、UPDATE(改)。主要用来对数据库重表的数据进行一些增删改操作。
  • DDL Data definition language 数据库定义语言
    就是我们在创建表的时候用到的一些sql,比如说:CREATE、ALTER、DROP等。主要是用在定义或改变表的结构,数据类型,表之间的链接和约束等初始化工作上。
  • DCL Data Control Language 数据库控制语言
    是用来设置或更改数据库用户或角色权限的语句,包括(grant(授予权限),deny(拒绝权限),revoke(收回权限)等)语句
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值