Mariadb 简明教程
MariaDB - Transactions
事务是连续的组操作。它们作为一个单一单元运行,并且在组内所有操作全部成功执行之前不会终止。组内只要有一个操作失败,整个事务就会失败,并且这不会对数据库产生任何影响。
Transactions are sequential group operations. They function as a single unit, and do not terminate until all operations within the group execute successfully. A single failure in the group causes the entire transaction to fail, and causes it to have no impact on the database.
事务符合 ACID(原子性、一致性、隔离性和耐久性)−
Transactions conform to ACID (Atomicity, Consistency, Isolation, and Durability) −
-
Atomicity − It ensures the success of all operations by aborting on failures and rolling back changes.
-
Consistency − It ensures the database applies changes on a successful transaction.
-
Isolation − It enables independent transactions operation of transactions.
-
Durability − It ensures the persistence of a successful transaction in the event of system failure.
在事务语句的开头是 START TRANSACTION 语句,后面跟着 COMMIT 和 ROLLBACK 语句 −
At the head of a transaction statement is the START TRANSACTION statement followed by COMMIT and ROLLBACK statements −
-
START TRANSACTION begins the transaction.
-
COMMIT saves changes to data.
-
ROLLBACK ends the transaction, destroying any changes.
在事务成功时,COMMIT 起作用。在失败时,ROLLBACK 起作用。
On a successful transaction, COMMIT acts. On a failure, ROLLBACK acts.
Note − 某些语句会导致隐式提交,当在事务内使用时,它们也会导致错误。此类语句的示例包括但不限于 CREATE、ALTER 和 DROP。
Note − Some statements cause an implicit commit, and they also cause an error when used within transactions. Examples of such statements include, but are not limited to CREATE, ALTER, and DROP.
MariaDB 事务还包括 SAVEPOINT 和 LOCK TABLES 等选项。SAVEPOINT 设置一个还原点以用于 ROLLBACK。LOCK TABLES 可以在会话期间控制对表的访问,以防止在某些时间段内进行修改。
MariaDB transactions also include options like SAVEPOINT and LOCK TABLES. SAVEPOINT sets a restore point to utilize with ROLLBACK. LOCK TABLES allows controlling access to tables during sessions to prevent modifications during certain time periods.
AUTOCOMMIT 变量提供对事务的控制。将其设置为 1 会强制所有操作都被视为成功事务,而将其设置为 0 则会导致只有在显式 COMMIT 语句中才会持久保留更改。
The AUTOCOMMIT variable provides control over transactions. A setting of 1 forces all operations to be considered successful transactions, and a setting of 0 causes persistence of changes to only occur on an explicit COMMIT statement.
Structure of a Transaction
事务语句的一般结构包含从 START TRANSACTION 开始。下一步是插入一个或多个命令/操作,插入检查错误的语句,插入 ROLLBACK 语句以管理发现的任何错误,最后插入 COMMIT 语句以对成功操作应用更改。
The general structure of a transaction statement consists of beginning with START TRANSACTION. The next step is inserting one or more commands/operations, inserting statements that check for errors, inserting ROLLBACK statements to manage any errors discovered and finally inserting a COMMIT statement to apply changes on successful operations.
查看下面给出的示例 −
Review the example given below −
START TRANSACTION;
SELECT name FROM products WHERE manufacturer = 'XYZ Corp';
UPDATE spring_products SET item = name;
COMMIT;