What if MySQL transactions contain DDL statements?

Source: Internet
Author: User

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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.