[MYSQL Notes] Using Transactions

Storage engine:

The functions of MYSQL are roughly divided into two types:

One: connect the client and check the content of the SQL statement in advance, that is, the foreground part

Two: According to the instructions of the foreground part, complete the functions of query and file operation, that is, the background part. This backend part is called the storage engine.

Types of storage engines:

MYSQL presets multiple storage engines, users can choose according to the purpose of use and personal preference. The existence of engines that are independent of each other and allow users to choose independently is the characteristic of MYSQL.

The engine InnoDB is used by default. But in versions up to MYSQL5.4, the default storage engine is MyISAM. Although MyISAM was faster than InnoDB at the time, it unfortunately did not support transactions.

But now InnoDB's processing speed has also become fast, so we don't need to use MyISAM.

Confirm the storage engine:

show create table tb;

We can confirm the storage engine in the ENGINE=XX section.

When creating a table, if no storage engine is specified, the default InnoDB will be selected.

Modify the storage engine:

Example: Change the storage engine of table tb from InnoDB to MyISAM

alter table tb engine=MyISAM;

Transactions: The ability to process multiple operations as a single logical unit of work is called a transaction.

The operation of reflecting the processing result after the transaction starts in the database is called commit.

An operation that is not reflected in the database but is restored to its original state is called a rollback.

Example: In a bank account, the deduction of 100,000 yuan and the addition of 100,000 yuan should be handled as an inseparable whole.

If the operation of adding 100,000 yuan fails, then the operation of deducting 100,000 yuan should also be canceled.

Open transaction:

start transaction;
delete from tb;
rollback;

After the table is dropped, it is rolled back (restored), and the deleted records can be restored.

When a rollback is performed, the transaction is closed.

In the above example, if commit is executed instead of rollback, the deleted records will be committed, and all records will be deleted by user records.

Auto-commit function: Execute commands in MYSQL, and the processing is usually submitted directly. That is, all commands automatically COMMIT.

By default, autocommit is turned on. However, when the storage engine is INNODB, if the start transaction is executed, it will not be committed before the commit command is executed.

Users can force autocommit to be off. If the auto-commit function is turned off, even if the SQL statement is executed, it will not be automatically committed. It must be committed through COMMIT or restored through rollback.

Turn off autocommit:

set autocommit=0;

To enable automatic submission:

set autocommit=1;

Scope of use of transactions:

After a transaction is started, not all operations can be recovered by rolling back.

The following operations will be automatically submitted

 

 

 

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124235274