What will happen to MySQL current transaction, if in the middle of that transaction, the DDL statement is executed?

MySQLMySQLi Database

The current MySQL transaction will be committed and ended when any of the DDL statement such as CREATE or DROP databases, Create, ALTER or DROP tables or stored routines is executed in the middle of the current transaction. All the database changes made in the current transaction will be made permanent and cannot be rolled back.

Example

mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO MARKS Values(6,'Manak','History',70);
Query OK, 1 row affected (0.26 sec)

mysql> Create table student(id int, Name Varchar(10),);
Query OK, 0 rows affected (0.84 sec)

As we can see in the example above, a DDL statement has been executed in the middle of a transaction hence this transaction will end implicitly. MySQL will save all the changes and it cannot be rolled back. We can observe it with the help of the following result set −

mysql> Rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from marks;
+------+---------+-----------+-------+
| Id   | Name    | Subject   | Marks |
+------+---------+-----------+-------+
| 1    | Aarav   | Maths     | 50    |
| 1    | Harshit | Maths     | 55    |
| 3    | Gaurav  | Comp      | 69    |
| 4    | Rahul   | History   | 40    |
| 5    | Yashraj | English   | 48    |
| 6    | Manak   | History   | 70    |
+------+---------+---------+---------+
6 rows in set (0.00 sec)
raja
Published on 22-Feb-2018 06:25:27
Advertisements