mysql (affairs)

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) :: atomic ( A tomicity, also known as indivisibility), consistency ( C onsistency), isolation ( the I solation, also known as independence), persistence ( D urability).

  • 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: database allows multiple concurrent transactions 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 can also use the COMMIT WORK, but the two are equivalent. COMMIT commits the transaction, and the database has been all modifications become permanent;

    • ROLLBACK can also use the 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 the transaction is rolled back to the marker;

    • SET TRANSACTION 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
    • Use a reserved spot SAVEPOINT

      The method is implemented savepoint "subtransactions" (subtransaction) in the database transaction processing, also known as nested transactions. Transactions can be rolled back to savepoint without affecting the changes before savepoint creation, does not need to give up the entire transaction.

      ROLLBACK Rollback usage can set the retention point SAVEPOINT, when performing a number of operations, before the statement that you want to roll back to.

      Use SAVEPOINT

      Savepoint_name SAVEPOINT ; // declare a savepoint  ROLLBACK the TO savepoint_name ; // rollback to savepoint    
       

      Delete SAVEPOINT

      Retention point and then automatically released after the transaction process is completed (execute a ROLLBACK or COMMIT).

      Since MySQL5, you can use:

      SAVEPOINT savepoint_name the RELEASE ; // delete the specified retention point  

Guess you like

Origin www.cnblogs.com/wuyujun/p/11184486.html