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 statements
in general, the transaction must meet four conditions (ACID) :: atomicity (Atomicity, also known as indivisibility), consistency (Consistency), isolation (Isolation, and He said 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:
the BEGIN or START TRANSACTION explicitly open a transaction;
a COMMIT may be used 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 The, SAVEPOINT allows you to create a save point in the transaction, a transaction can have multiple SAVEPOINT;
the RELEASE SAVEPOINT identifier The deletion of a transaction savepoint when not specified save point, execute the statement will throw an exception;
rOLLBACK the transaction is rolled back to the tO identifier the marker;
the 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 MySQL Tutorial MySQL Tutorial MySQL installation MySQL Management MySQL PHP syntax MySQL connection MySQL database to create a MySQL database delete MySQL select the database MySQL data types MySQL create a data table MySQL delete data table MySQL inserting data MySQL query data MySQL WHERE clause of MySQL uPDATE update MySQL dELETE statement MySQL LIKE clause MySQL UNION MySQL sort MySQL grouping with MySQL connections MySQL NULL value processing MySQL regular expression type MySQL Services MySQL ALTER command MySQL index MySQL temporary table MySQL replication table MySQL metadata MySQL sequence using MySQL duplicate data processing MySQL and SQL injection MySQL data export MySQL data import MySQL function MySQL operator MySQL regex the ALTER command MySQL MySQL transaction MySQL transaction data operation mainly for large, 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 statements in general, the transaction must meet four conditions (ACID) :: atomicity (Atomicity, also known as indivisibility), consistency (Consistency), isolation (Isolation, and He said 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 the AUTOCOMMIT Run the SET = 0, used to disable the auto-commit the current session. Transaction control statements: the BEGIN or START TRANSACTION explicitly open a transaction; a COMMIT may be used 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 The, 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 the transaction is rolled back to the TO identifier The marker; the 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 methods: 1 , with BEGIN, ROLLBACK, COMMIT achieved BEGIN and a transaction ROLLBACK transaction rollback COMMIT acknowledgment transaction 2 , changing the SET directly MySQL autocommit mode: SET the AUTOCOMMIT = 0 to disable automatic submission aUTOCOMMIT the SET = 1 enable auto-commit
mysql> use RUNOOB; Database changed mysql> CREATE TABLE runoob_transaction_test( id int(5)) engine=innodb; # 创建数据表 Query OK, 0 rows affected (0.04 sec) mysql> select * from runoob_transaction_test; Empty set (0.01 sec) mysql> begin; # 开始事务 Query OK, 0 rows affected (0.00 sec) mysql> insert into runoob_transaction_test value(5); Query OK, 1 rows affected (0.01 sec) mysql> insert into runoob_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 runoob_transaction_test;
MySQL> the begin; # start affairs Query OK, 0 rows affected (0.00 sec) MySQL > INSERT INTO runoob_transaction_test values (7 ); Query the OK, 1 rows affected (0.00 sec) MySQL > ROLLBACK; # rollback Query OK, 0 rows affected (0.00 sec) MySQL > SELECT * from runoob_transaction_test; # because the data is not inserted so rollback
PHP is used transaction instance < PHP? $ Dbhost = ' localhost: 3306 ' ; // MySQL server host address $ dbuser = ' root ' ; // MySQL username $ dbpass = ' 123456 ' ; // MySQL username and password $ conn = mysqli_connect ($ dbHost, dbuser $, $ dbpass); IF ($ Conn!) { Die ( ' connection failed: ' . mysqli_error ($ Conn)); } // set the encoding, Chinese distortion preventing the mysqli_query ($ Conn, " utf8 names the SET "); Mysqli_select_db ($ conn, ' RUNOOB ' ); mysqli_query ($ conn, " the SET AUTOCOMMIT = 0 " ); // set to not automatically submit, because MYSQL default execution immediately mysqli_begin_transaction ($ conn); // start transaction definition if (! the mysqli_query ($ Conn, " INSERT INTO runoob_transaction_test (ID) values (. 8) " )) { the mysqli_query ($ Conn, " rOLLBACK " ); // rollback determination fails when } iF (the mysqli_query (Conn $,! " INSERT INTO runoob_transaction_test (the above mentioned id) values (9) " )) { mysqli_query ($ conn, " ROLLBACK " ); // rollback judge fails } the mysqli_commit ($ conn); // execute transactions mysqli_close ($ conn); ? >