START TRANSACTION

This statement starts a new transaction inside of TiDB. It is similar to the statement BEGIN.

In the absence of a START TRANSACTION statement, every statement will by default autocommit in its own transaction. This behavior ensures MySQL compatibility.

Synopsis

BeginTransactionStmt:

BeginTransactionStmt

START TRANSACTION - 图1

AsOfClause

START TRANSACTION - 图2

  1. BeginTransactionStmt ::=
  2. 'BEGIN' ( 'PESSIMISTIC' | 'OPTIMISTIC' )?
  3. | 'START' 'TRANSACTION' ( 'READ' ( 'WRITE' | 'ONLY' ( ( 'WITH' 'TIMESTAMP' 'BOUND' TimestampBound )? | AsOfClause ) ) | 'WITH' 'CONSISTENT' 'SNAPSHOT' | 'WITH' 'CAUSAL' 'CONSISTENCY' 'ONLY' )?
  4. AsOfClause ::=
  5. ( 'AS' 'OF' 'TIMESTAMP' Expression)

Examples

  1. mysql> CREATE TABLE t1 (a int NOT NULL PRIMARY KEY);
  2. Query OK, 0 rows affected (0.12 sec)
  3. mysql> START TRANSACTION;
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql> INSERT INTO t1 VALUES (1);
  6. Query OK, 1 row affected (0.00 sec)
  7. mysql> COMMIT;
  8. Query OK, 0 rows affected (0.01 sec)

MySQL compatibility

  • START TRANSACTION immediately starts a transaction inside TiDB. This differs from MySQL, where START TRANSACTION lazily creates a transaction. But START TRANSACTION in TiDB is equivalent to MySQL’s START TRANSACTION WITH CONSISTENT SNAPSHOT.

  • The statement START TRANSACTION READ ONLY is parsed for compatibility with MySQL, but still allows write operations.

See also