BEGIN

This statement starts a new transaction inside of TiDB. It is similar to the statements START TRANSACTION and SET autocommit=0.

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

Synopsis

BeginTransactionStmt

BEGIN - 图1

  1. BeginTransactionStmt ::=
  2. 'BEGIN' ( 'PESSIMISTIC' | 'OPTIMISTIC' )?
  3. | 'START' 'TRANSACTION' ( 'READ' ( 'WRITE' | 'ONLY' ( 'WITH' 'TIMESTAMP' 'BOUND' TimestampBound )? ) | 'WITH' 'CONSISTENT' 'SNAPSHOT' )?

Examples

  1. mysql> CREATE TABLE t1 (a int NOT NULL PRIMARY KEY);
  2. Query OK, 0 rows affected (0.12 sec)
  3. mysql> BEGIN;
  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

TiDB supports the syntax extension of BEGIN PESSIMISTIC or BEGIN OPTIMISTIC. This enables you to override the default transactional model for your transaction.

See also