MySQL transaction summary

Transaction Basic Characteristics (ACID)

  1. Atomicity: After the transaction starts, all operations are either done or not done, and it is impossible to stagnate in the middle. If an error occurs during the execution of the transaction, it will be rolled back to the state before the transaction started, and all operations will be as if nothing happened. That is to say, affairs are an indivisible whole, just like atoms learned in chemistry, which are indivisible units of matter.
  2. Consistency: The integrity constraints of the database are not violated before and after the transaction begins and ends.
  3. Isolation: At the same time, only one transaction is allowed to request the same data, and different transactions do not interfere with each other.
  4. Durability: After the transaction is completed, all updates to the database by the transaction will be saved to the database and cannot be rolled back.

transaction statement

MySQL uses the following statement to identify the start of a transaction:

start transaction

or

begin

After the transaction is successful, use the following statement to identify the success of the transaction:

commit

When an exception occurs in the transaction logic, the following statement can be used to identify the cancellation of the transaction:

rollback

transaction operation

Transaction processing is used to manage INSERT, UPDATE, and DELETE statements. It cannot manage SELECT queries that do not change data, and it cannot manage CREATE or DROP operations. Transaction operations isolate the visibility of the state changes of the operated data, and at the same time make multiple data update operations within a transaction atomic, that is, either succeed at the same time or fail at all.

Create test table test:

create table test(a int, b char(5));

To perform a transaction operation:

start transaction;

insert into test values('1', 'right');
insert into test values('2', 'wrong');
update test set b = 'right' where a = 2;
select * from test;

commit;

In the above operations, one transaction contains two independent insert operations, one update operation, and one query operation for all data. Due to the isolation of the transaction, before the end of the transaction, the query statements of other connections will not be able to query the two inserted data, nor will they be able to update them. Only the query statement and update statement in the same transaction can target the newly inserted data. query or update the data. When the commit statement is executed, all operations in the transaction will take effect, so that other connections can also operate on the updated data of this transaction.

When the statement in the above transaction fails to execute, or the rollback statement is executed before the transaction is committed, no matter how many statements have been successfully executed, the database state will be restored to the state before all the statements were executed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325449559&siteId=291194637