Home > Database > Mysql Tutorial > 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
Release: 2023-09-03 10:41:02
forward
1280 people have browsed it

如果在当前事务的中间执行 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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template