Get the App
SLTechnology News&Howtos  ›  Database  › 

Three minutes to master the handling of transactions in MySQL

Shulou Source: shulou.com Published: 2022-06-01 18:45:42 09月18日 Update

This article mainly introduces the handling of transactions in MySQL, the contents of the articles are carefully selected and edited by the author, with a certain pertinence, the reference significance for everyone is still relatively great, the following with the author to understand the handling of transactions in MySQL.

Steps:

1. Open transaction start transaction

When we start a transaction, our operations on sql take place in memory, but there is no real feedback to the file on the database disk!

two。 Roll back rollback

Rollback is to return to the original state before the transaction starts!

Note: the rollback operation will automatically close a transaction. If you want to execute the transaction again, you need to reopen the transaction!

3. Submit commit

The basic principles of transactions

Ordinary execution is executed immediately and takes effect, because by default, MySQL's execution of sql statements is automatically committed! Therefore, the essence of opening a transaction is to turn off the previous auto-commit function, but by the user to manually commit (using the commit statement)!

Steps to summarize the transaction:

1. Start the transaction

2. If the execution is successful, submit the commit.

3. If any of the sql statements fail, roll back rollback!

The most typical transaction is borrowing and repaying money. Let's take Zhang San returning 1000 yuan to Li Si as an example.

First check the respective amount of money in the database

Here is the code to handle the repayment transaction:

Tags: Transaction statement success data processing database Li Si that is status result Zhang San original content field article step author change ordinary reason Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information Apple Shulou Tech Info Redmi