What if MySQL transactions contain DDL statements?
As we all know, MySQL DDL statements are non-transactional, that is, DLL statements cannot be rolled back. What if a transaction contains DDL statements?
For example:
#Disable auto commit
set autocommit=off;
Create tb1
create table tb1(id int auto_increment primary key,c1 int);
#Start transaction
start transaction;
#Insert data
insert into tb1(c1) select 1;
insert into tb1(c1) select 2;
insert into tb1(c1) select 3;
Create TB2
create table tb2(id int auto_increment primary key,c1 int);
If rollback is executed, data inserted into tb1 is not rolled back:
If you view binlog:
Before creating tb2, a commit operation commits the transactions in the preceding statement. Therefore, although the rollback operation is executed later, the rollback operation is actually another empty transaction, therefore, no data is rolled back ".
When executing the scripts submitted by R & D colleagues, the DDL and DML statements and DCL statements should be strictly separated to prevent the transactions from being implicitly "damaged" and lead to misoperations.
For example, if you open a transaction and are modifying the data, the R & D colleague will ask you to create a new table in this database. After cutting the data, you will find that your data has been changed incorrectly. Now, it is a tragedy to roll back the data!
For example, if a transaction script is developed and a DDL statement is written in the middle, the transaction is committed in the middle. When an exception is found to be rolled back, only a part of the transaction can be rolled back.
Summary:
When a DDL statement is executed, the current session transaction is implicitly "COMMIT". Therefore, when a DDL statement is executed in MySQL, DDL and DML should be strictly separated and cannot be executed together.