select*frommarks;+------+----------+-----------+-- -----+|Id |Name |Subject |Marks|+------+-----"/> select*frommarks;+------+----------+-----------+-- -----+|Id |Name |Subject |Marks|+------+-----">

Home  >  Article  >  Database  >  What happens to the current MySQL transaction if a START TRANSACTION command is executed in the middle of the current transaction?

What happens to the current MySQL transaction if a START TRANSACTION command is executed in the middle of the current transaction?

WBOY
WBOYforward
2023-09-03 10:41:021276browse

如果在当前事务的中间执行 START TRANSACTION 命令,当前 MySQL 事务会发生什么?

If START TRANSACTION is executed in the middle of the current transaction, the current transaction will be committed and ended. All database changes made during the current transaction are made permanent. This is called an implicit commit of the START TRANSACTION command.

Example

Suppose we have the following values ​​in table "marks"

mysql> select * from marks;
+------+---------+-----------+-------+
| Id   | Name    | Subject   | Marks |
+------+---------+-----------+-------+
| 1    | Aarav   | Maths     | 50    |
| 1    | Harshit | Maths     | 55    |
| 3    | Gaurav  | Comp      | 69    |
+------+---------+-----------+-------+
3 rows in set (0.00 sec)

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

mysql> INSERT INTO Marks Values(4, 'Rahul','History',40);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Marks Values(5, 'Yashraj','English',48);
Query OK, 1 row affected (0.00 sec)

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

In this example, we can observe that when the START TRANSACTION statement is executed in the middle of the current transaction , it will implicitly end the current transaction and commit the changes.

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    |
+------+---------+-----------+-------+
5 rows in set (0.00 sec)

The above is the detailed content of What happens to the current MySQL transaction if a START TRANSACTION command is executed in the middle of the current transaction?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete