Leilin Peng Share: MySQL Affairs

  MySQL transaction is mainly used for large data manipulation, high complexity of the process. For example, in personnel management system, you delete a person, that is, you need to remove the basic information of the staff, and the staff have to delete related information, such as mail, articles, etc., so that the database operation statement constitutes a transaction !

  Only in MySQL database engine used Innodb database or table only support transactions.

  Transactions can be used to maintain the integrity of the database to ensure that the bulk of the SQL statements either all executed or not executed all.

  Transaction management for insert, update, delete statement

  Generally speaking, the transaction must meet four conditions (ACID) :: atomicity (Atomicity, also known as indivisibility), consistency (Consistency), isolation (Isolation, also known as independence), persistent (Durability) .

  Atomicity: All operations in one transaction (transaction) is either completed or not completed all, does not end in the middle of a link. Transaction error occurs during execution, it will be rolled back (Rollback) to the state before the start of the transaction, as the transaction never performed the same.

  Consistency: before the transaction begins and after the end of the transaction, the integrity of the database is not corrupted. This means that data written must be fully compliant with all of the default rules, which include data accuracy, and a series of follow-up database can complete the scheduled work spontaneously.

  Isolation: a plurality of concurrent transactions database allows simultaneous read and write capabilities and modify its data, the isolation can be prevented when a plurality of transactions concurrently perform cross executed result in inconsistent data. Transaction isolation divided into different levels, including Uncommitted Read (Read uncommitted), Read Committed (read committed), repeatable read (repeatable read) and serialized (Serializable).

  Durability: After a transaction, changes to data is permanent, even if the system failure will not be lost.

  In the default setting MySQL command line transactions are automatically submitted after that is to execute SQL statements COMMIT operation will be executed immediately. Therefore explicitly advised to open a transaction using the BEGIN command or START TRANSACTION, or execute the command SET AUTOCOMMIT = 0, used to disable the auto-commit the current session.

  Transaction control statements:

  BEGIN or START TRANSACTION; explicitly open a transaction;

  COMMIT; may also be used COMMIT WORK, but the two are equivalent. COMMIT commits the transaction, and the database has been all modifications become permanent;

  ROLLBACK; there may be used ROLLBACK WORK, but the two are equivalent. Rollback end user transaction and to withdraw all uncommitted changes under way;

  SAVEPOINT identifier; SAVEPOINT allows you to create a save point in the transaction, a transaction can have multiple SAVEPOINT;

  RELEASE SAVEPOINT identifier; delete a transaction save point, when there is no designated save point, execute the statement will throw an exception;

  ROLLBACK TO identifier; roll back the transaction to the marker;

  SET TRANSACTION; used to set the transaction isolation level. InnoDB storage engine provides the transaction isolation level have READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, and SERIALIZABLE.

  MYSQL transaction There are two main methods:

  1, with BEGIN, ROLLBACK, COMMIT to achieve

  BEGIN start a transaction

  ROLLBACK transaction rollback

  COMMIT transaction confirmation

  2, the direct use SET to change MySQL's auto-commit mode:

  SET AUTOCOMMIT = 0 to disable automatic submission

  SET AUTOCOMMIT = 1 enable auto-commit

  Testing Services

  mysql> use CODERCTO; Database changed mysql> CREATE TABLE codercto_transaction_test( id int(5)) engine=innodb; # 创建数据表 Query OK, 0 rows affected (0.04 sec) mysql> select * from codercto_transaction_test; Empty set (0.01 sec) mysql> begin; # 开始事务 Query OK, 0 rows affected (0.00 sec) mysql> insert into codercto_transaction_test value(5); Query OK, 1 rows affected (0.01 sec) mysql> insert into codercto_transaction_test value(6); Query OK, 1 rows affected (0.00 sec) mysql> commit; # 提交事务 Query OK, 0 rows affected (0.01 sec) mysql> select * from codercto_transaction_test; +------+ | id | +------+ | 5 | | 6 | +------+ 2 rows in set (0.01 sec) mysql> begin; # 开始事务 Query OK, 0 rows affected (0.00 sec) mysql> insert into codercto_transaction_test values(7); Query OK, 1 rows affected (0.00 sec) mysql> rollback;# Rollback Query OK, 0 rows affected (0.00 sec) mysql> select * from codercto_transaction_test; # because the data is not inserted so rollback + ------ + | id | + ------ + | 5 | | 6 | + ------ + 2 rows in set (0.01 sec) mysql>

  PHP is used transaction instance

  MySQL ORDER BY tests:

  

  Click to see all MySQL Tutorial Articles: https://www.codercto.com/courses/l/30.html (edit: Leilin Peng Source: network intrusion deleted)

Guess you like

Origin www.cnblogs.com/pengpeng1208/p/11016296.html