8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 87%

15.3.7 SET TRANSACTION 语句15.3.7 SET TRANSACTION Statement

SET [GLOBAL | SESSION] TRANSACTION
    transaction_characteristic [, transaction_characteristic] ...

transaction_characteristic: {
    ISOLATION LEVEL level
  | access_mode
}

level: {
     REPEATABLE READ
   | READ COMMITTED
   | READ UNCOMMITTED
   | SERIALIZABLE
}

access_mode: {
     READ WRITE
   | READ ONLY
}

该语句指定了 事务特征。它取一个或多个特征值的列表,并用逗号分隔。每个特征值都设置事务隔离级别或访问模式。隔离层用于操作 InnoDB 表。访问模式指定事务是以读写模式运行还是只读模式。This statement specifies transaction characteristics. It takes a list of one or more characteristic values separated by commas. Each characteristic value sets the transaction isolation level or access mode. The isolation level is used for operations on InnoDB tables. The access mode specifies whether transactions operate in read/write or read-only mode.

此外,SET TRANSACTION包含可选的 GLOBAL 或 SESSION 关键字来指示语句的作用域。In addition, SET TRANSACTION can include an optional GLOBAL or SESSION keyword to indicate the scope of the statement.

事务隔离级别Transaction Isolation Levels

要设置事务隔离级别,请使用 ISOLATION LEVEL level 子句。在相同的 ISOLATION LEVEL 语句中不允许指定多个 SET TRANSACTION 子句。To set the transaction isolation level, use an ISOLATION LEVEL level clause. It is not permitted to specify multiple ISOLATION LEVEL clauses in the same SET TRANSACTION statement.

默认隔离级别是 REPEATABLE READ。其他允许的值包括 READ COMMITTED、READ UNCOMMITTED 和 SERIALIZABLE。有关这些隔离级别的信息,请参阅 第 17.7.2.1 节,“事务隔离级别”。The default isolation level is REPEATABLE READ. Other permitted values are READ COMMITTED, READ UNCOMMITTED, and SERIALIZABLE. For information about these isolation levels, see Section 17.7.2.1, “Transaction Isolation Levels”.

事务访问模式Transaction Access Mode

要设置事务访问模式,请使用 READ WRITE 或 READ ONLY 子句。在相同的 SET TRANSACTION 语句中不允许指定多个访问模式子句。To set the transaction access mode, use a READ WRITE or READ ONLY clause. It is not permitted to specify multiple access-mode clauses in the same SET TRANSACTION statement.

默认情况下,事务以读写模式进行,允许对事务中使用的表进行读写操作。可以使用 SET TRANSACTION 并带有 READ WRITE 访问模式显式指定此模式。By default, a transaction takes place in read/write mode, with both reads and writes permitted to tables used in the transaction. This mode may be specified explicitly using SET TRANSACTION with an access mode of READ WRITE.

如果事务访问模式设置为 READ ONLY,则禁止对表进行更改。这可能使存储引擎在写操作不允许时进行性能优化。If the transaction access mode is set to READ ONLY, changes to tables are prohibited. This may enable storage engines to make performance improvements that are possible when writes are not permitted.

在只读模式下,仍可使用 DML 语句更改使用TEMPORARY。不允许对 DDL 语句进行更改,与永久表相同。In read-only mode, it remains possible to change tables created with the TEMPORARY keyword using DML statements. Changes made with DDL statements are not permitted, just as with permanent tables.

READ WRITE 和 READ ONLY 访问模式也可以使用 START TRANSACTION 语句为单个事务指定。The READ WRITE and READ ONLY access modes also may be specified for an individual transaction using the START TRANSACTION statement.

事务特性范围Transaction Characteristic Scope

您可以全局设置事务特性、针对当前会话设置,或仅针对下一事务进行设置:You can set transaction characteristics globally, for the current session, or for the next transaction only:

  • 使用 GLOBAL 关键字:With the GLOBAL keyword:

    • 该语句对所有后续会话全局适用。The statement applies globally for all subsequent sessions.

    • 现有会话不受影响。Existing sessions are unaffected.

  • 使用 SESSION 关键字:With the SESSION keyword:

    • 该语句适用于当前会话内执行的所有后续事务。The statement applies to all subsequent transactions performed within the current session.

    • 该语句允许在事务内使用,但不影响当前正在进行的事务。The statement is permitted within transactions, but does not affect the current ongoing transaction.

    • 如果在事务之间执行,该语句将覆盖任何先前设置命名特性的下一事务值的语句。If executed between transactions, the statement overrides any preceding statement that sets the next-transaction value of the named characteristics.

  • 如果没有任何 SESSION 或 GLOBAL 关键字:Without any SESSION or GLOBAL keyword:

    • 该语句仅适用于会话中执行的下一次单个事务。The statement applies only to the next single transaction performed within the session.

    • 随后的事务将恢复使用命名特性的会话值。Subsequent transactions revert to using the session value of the named characteristics.

    • 事务中不允许使用该语句:The statement is not permitted within transactions:

      mysql> START TRANSACTION;
      Query OK, 0 rows affected (0.02 sec)
      
      mysql> SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
      ERROR 1568 (25001): Transaction characteristics can't be changed
      while a transaction is in progress
      

更改全局事务特性需要 CONNECTION_ADMIN 权限(或已弃用的 SUPER 权限)。任何会话都可以更改其会话特性(即使在事务中途)或其下一事务的特性(在该事务开始之前)。A change to global transaction characteristics requires the CONNECTION_ADMIN privilege (or the deprecated SUPER privilege). Any session is free to change its session characteristics (even in the middle of a transaction), or the characteristics for its next transaction (prior to the start of that transaction).

要在服务器启动时设置全局隔离级别,请在命令行或选项文件中使用 --transaction-isolation=level 选项。此选项中 level 的值使用连字符而不是空格,因此允许的值为 READ-UNCOMMITTED、READ-COMMITTED、REPEATABLE-READ 或 SERIALIZABLE。To set the global isolation level at server startup, use the --transaction-isolation=level option on the command line or in an option file. Values of level for this option use dashes rather than spaces, so the permissible values are READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, or SERIALIZABLE.

类似地,要在服务器启动时设置全局事务访问模式,请使用 --transaction-read-only 选项。默认值为 OFF(读/写模式),但可以将其设置为 ON 以启用只读模式。Similarly, to set the global transaction access mode at server startup, use the --transaction-read-only option. The default is OFF (read/write mode) but the value can be set to ON for a mode of read only.

例如,要将隔离级别设置为 REPEATABLE READ 并将访问模式设置为 READ WRITE,可在选项文件的 [mysqld] 部分中使用以下行:For example, to set the isolation level to REPEATABLE READ and the access mode to READ WRITE, use these lines in the [mysqld] section of an option file:

[mysqld]
transaction-isolation = REPEATABLE-READ
transaction-read-only = OFF

在运行时,全局、会话和下一事务范围级别的特性可以如前所述通过 SET TRANSACTION 语句间接设置。也可以通过 SET 语句直接为 transaction_isolation 和 transaction_read_only 系统变量赋值:At runtime, characteristics at the global, session, and next-transaction scope levels can be set indirectly using the SET TRANSACTION statement, as described previously. They can also be set directly using the SET statement to assign values to the transaction_isolation and transaction_read_only system variables:

下表显示了每个SET TRANSACTION和变量赋值语法设置的特征作用域级别。The following tables show the characteristic scope level set by each SET TRANSACTION and variable-assignment syntax.

表 15.9 事务特征的语法Table 15.9 SET TRANSACTION Syntax for Transaction Characteristics

语法Syntax 集合 影响的特征范围Affected Characteristic Scope
SET GLOBAL TRANSACTION transaction_characteristic 全球Global
SET SESSION TRANSACTION transaction_characteristic 会议Session
SET TRANSACTION transaction_characteristic 下一个仅限事务Next transaction only

的表 15.10 SET 事务特征Table 15.10 SET Syntax for Transaction Characteristics

语法Syntax 集合 影响的特征范围Affected Characteristic Scope
SET GLOBAL var_name = value 全球Global
SET @@GLOBAL.var_name = valueSET @@GLOBAL.var_name = value 全球Global
SET PERSIST var_name = value 全球Global
SET @@PERSIST.var_name = valueSET @@PERSIST.var_name = value 全球Global
SET PERSIST_ONLY var_name = value 无运行时影响No runtime effect
SET @@PERSIST_ONLY.var_name = valueSET @@PERSIST_ONLY.var_name = value 无运行时影响No runtime effect
SET SESSION var_name = value 会议Session
SET @@SESSION.var_name = valueSET @@SESSION.var_name = value 会议Session
SET var_name = value 会议Session
SET @@var_name = value 下一个仅限事务Next transaction only

可以在运行时检查事务特性的全局和会话值:It is possible to check the global and session values of transaction characteristics at runtime:

SELECT @@GLOBAL.transaction_isolation, @@GLOBAL.transaction_read_only;
SELECT @@SESSION.transaction_isolation, @@SESSION.transaction_read_only;