MySQL must know will be - Transaction Management Chapter 26 study notes

This chapter describes what the transaction, how to use commit and rollback statements to manage transactions.

1 What is a transaction
that not all engines support transactions.
MyISAM and InnoDB engine are two most commonly used.
MyISAM does not support transaction management.
InnoDB supports transaction management.
Transaction processing If your application needs, be sure to use the correct type of engine.
Transaction processing is used to maintain the integrity of the database, which ensures that the bulk of the MySQL operations either fully executed or not executed at all.
Relational databases are designed to store data in a plurality of tables, making the data easier to manipulate, maintenance and reuse. In a way, well-designed database schema are associated.
Orders previous chapters using a table is a good example. Orderitems in orders and orders are stored in two tables: orders to store the actual orders, and orderitems store ordered a variety of items. The two tables using the unique ID called the primary key related to each other. The two tables and other tables containing customer and product information associated.
Add an order to the system as follows:
if there is a corresponding customer (queries from the customers table), if not, add the client ① Check the database.
② retrieve customer's ID.
③ add a row to the orders table, associating it with the customer ID.
④ retrieve the orders table gives new order ID.
For each ordered item ⑤ add a row to the table orderitems by the retrieved ID orders table associated with it.
Now, if for some database failure (such as out of disk space, security restrictions, table locks, etc.) prevented the completion of this process, the data in the database will be what happens?
If a failure occurs after adding a client, before adding the orders table, there would be no problem. Some customers did not order is perfectly legal. In this re-execution process, the inserted customer record will be retrieved and used. Can effectively start this procedure from the place failed.
But if a failure occurs after adding orders row, before adding orderitems line, how to do it? Now, there is an empty database in order.
Worse system failure in adding orderitems line, the results are incomplete orders exist in the database, and you do not know.
How to solve this problem? This requires the use of a transaction. Transaction processing is a mechanism used to manage must MySQL batch operations performed , in order to ensure that the database does not contain complete operating results.
Use of transaction processing, a set of operations can be guaranteed not stopped halfway, they or executed as a whole, or not performed . If no error occurs, the entire group of statements submitted to the (written) database table. If an error occurs, the rollback (undo) to restore the database to a known and safe state.
So, look at the same example, this time we explain the process of how it works:
① check whether there is a corresponding database of customers, if not, add the customer.
② submit customer information.
③ retrieve customer's ID.
④ add a line to the orders table.
⑤ If an error occurs when you add a row to the orders table, rollback.
⑥ retrieve the new order ID orders given in the table.
⑦ ordered for each item, add a new row to the table orderitems.
⑧ If you add a new row to the failure orderitems, roll back any orderitems rows and rows orders added.
⑨ submit order information.
When using transactions and transaction processing, there are a few key words repeated. Here are a few terms you need to know about the transaction:
① affairs: a set of SQL statements
② rollback: revoke the designation process SQL statements
③ submission: The SQL statement result is not stored into the database table
④ retention point: Transaction Processing set temporary placeholders , you can publish on his back-off (and back the entire transaction process different)

2 control transactions
discuss management of the transaction involved the following. The key management transaction that will decompose SQL statements as a logical block group, and specify the data to be rolled back when, when it should not be rolled back. MySQL use the following statement to identify the transaction begins:

start transaction

(1) using the rollback
MySQL's rollback command to roll back (undo) MySQL statement:

select * from ordertotals;
start transaction;
delete from ordertotals;
select * from ordertotals;
rollback;
select * from ordertotals;

The example starts from the content displayed ordertotals table.
First, perform a select statement to show that the table is not empty.
Then start a transaction, delete all the rows in ordertotals with a delete statement. Another select statement to verify ordertotals indeed empty.
Then with a rollback statement to roll back all statements after the start transaction.
Finally, a select statement shows that the table is not empty.
rollback only be used within a transaction (in the implementation of a start transaction command after).
What statement can be rolled back:
Transaction management for insert, update, delete statement.
Select statement can not be rolled back (rollback select statement does not make sense).
Rollback can not create or drop operation.
Transaction processing block may be used create or drop operation, if you perform avoidance, create or drop is not undone.

(2) using commit
general statements are directed against MySQL database tables and execute written submission (write or save) operation is automatic. This is the implicit submission .
In a transaction block, filed will not be performed implicitly . For specific submission, to use commit statement, as follows:

start transaction;
delete from orderitems where order_num=20010;
delete from orders where order_num=20010;
commit;

In this example, the order to completely remove 20010 from the system. Because it involves updating two database tables orders and orderitems, so the use of block transactions to ensure that orders are not deleted portions.
The last commit statement only when it is not wrong to write the changes.
If the first delete function, but the second fails, delete does not submit (in fact, it is automatically revoked).
Implicit transaction closed: When commit or rollback statement is executed, the transaction will automatically shut down .
(3) using the reserved spot
simple rollback and commit statement can be written or revoke the entire transaction process .
But to do so for simple transactions, more complex transaction may need to partially committed or rolled back .
For example, add an order of the process described above is a transaction. If an error occurs, you can add only need to return, customers do not need to fall back to the table before the orders line.
To support part of the transaction is rolled back, it must be replaced with a placeholder in the transaction processing block in place. If you need a fallback, you can fall back to a placeholder. These placeholders called retention point. To create a placeholder, use the savepoint statement:

savepoint delete1;

Reservations are taken to identify every point it's only the name, so that when the rollback, MySQL know where to return retreated. In order to fall back to the retention point of the present embodiment given, can be carried out as follows:

rollback to delete1;

Retention point better. You can set any number of retention points in the MySQL code, the better . Because the more reserved point, you can flexibly according to their own wishes rolled back.
Release retention point: Reserved point automatically released after the transaction is completed (perform a rollback or commit). Since MySQL5, can also be reserved expressly release point with release savepoint.
(4) Change the default commit behavior
default behavior of MySQL is automatically submit all changes (at any time you execute a MySQL statement that are actually executed against the table, and the changes take effect immediately). To indicate that MySQL does not automatically commit the changes, you need to use the following statement:

set autocommit=0;

autocommit flag determines whether to automatically submit the changes, with or without commit statement . Setting autocommit of 0 indicates that MySQL does not automatically submit the changes (until autocommit is set to true so far).
Flag dedicated connection: autocommit flag is for each connection rather than the server.

Summary
This chapter describes the transaction is complete SQL statement block must be executed. We learned how to use commit and rollback statement on when writing data, when the revocation of a clear management. We also learned how to use the reserved points provide greater control of the fallback operation.

Published 90 original articles · won praise 8 · views 8221

Guess you like

Origin blog.csdn.net/weixin_43854189/article/details/104076772