How can a user implicitly end the current MySQL transaction?

WBOY
Release: 2023-08-29 22:57:02
forward
826 people have browsed it

用户如何隐式结束当前的 MySQL 事务?

The following is how to implicitly end the current MySQL transaction -

By running the DDL statement

The current MySQL transaction will be implicitly ended, And the changes will be committed by running any DDL statement (such as CREATE or DROP database, Create, ALTER or DROP table or stored routine). This is because, in MySQL, these statements cannot be rolled back.

Example

mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO MARKS Values(3,'gaurav','Comp',69); 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)
Copy after login

Between transactions we run DDL statements, so the transaction will end implicitly. MySQL will save all changes and cannot roll 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 | +------+---------+-----------+-------+ 3 rows in set (0.00 sec)
Copy after login

By disconnecting the client session

Disconnecting the client session will implicitly end the current session, in which case the changes will be reverted roll.

By terminating the client session < /h2>

Terminating the client session will also implicitly end the current session, in which case the changes will also be rolled back.

Via the START TRANSACTION command

Currently if we run the START TRANSACTION command, the transaction will end implicitly. In this case, the changes will be committed.

Example

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, the START TRANSACTION statement will implicitly end the 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 How can a user implicitly end the current MySQL 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!