Home > Database > Mysql Tutorial > How does a user start a new MySQL transaction?

How does a user start a new MySQL transaction?

WBOY
Release: 2023-08-24 23:41:16
forward
877 people have browsed it

用户如何开始新的 MySQL 事务?

Users can start a new MySQL transaction by running the command START TRANSACTION. The behavior of the transaction will depend on the SQL AUTOCOMMIT mode. The default mode is "AUTOCOMMIT ON" mode, where each MySQL statement is treated as a complete transaction and commits by default on completion. It can be started by setting the session variable AUTOCOMMIT to 1 as shown below -

SET AUTOCOMMIT = 1

mysql> SET AUTOCOMMIT = 1;
Query OK, 0 rows affected (0.07 sec)
Copy after login

If the user wants to change this behavior of MySQL transactions, then he/she can set the "AUTOCOMMIT OFF" SQL mode, in In this mode, the subsequent series of MySQL statements acts like a transaction, and no activity is committed until an explicit COMMIT statement has been issued. In this mode, the first executable statement of a new session implicitly starts a new multi-statement transaction. It can be started by setting the session variable AUTOCOMMIT to 0 as shown below -

SET AUTOCOMMIT = 0

mysql> SET AUTOCOMMIT = 0;
Query OK, 0 rows affected (0.00 sec)
Copy after login

For transactions in InnoDB, do not use SET AUTOCOMMIT = 0, instead use the COMMIT command to commit.

In both SQL modes, transactions will be started using the START TRANSACTION command, as follows -

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

In fact, the above query informs MySQL that the following statements should be treated as a single unit of work , until the transaction ends.

The above is the detailed content of How does a user start a new 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template