START TRANSACTION [with consistent snapshot]begin [work]commit [Work] [and [No] CHAIN] [[No] release]rollback [work] [and [NO] CHAIN] [[NO] Release]set autocommit = {0 | 1}
These statements provide control over use of transactions:
START TRANSACTION
Or BEGIN
start a new transaction.
COMMIT
Commits the current transaction, making its changes permanent.
ROLLBACK
Rolls back the current transaction, canceling its changes.
SET autocommit
Disables or enables the default autocommit mode for the current session.
By default, MySQL runs with autocommit mode enabled. This means is soon as you execute a statement that updates (modifies) a table, MySQL stores the update in disk to MAK E it permanent. The change cannot is rolled back.
To disable autocommit mode implicitly for a single series of statements, use the START TRANSACTION
statement:
START TRANSACTION; SELECT @a:=sum (Salary) from table1 WHERE type=1; UPDATE table2 SET [email protected] WHERE type=1; COMMIT;
START TRANSACTION
with, autocommit remains disabled until your end the transaction with COMMIT
or ROLLBACK
. The autocommit mode then reverts to its previous state.
Can also begin a transaction like this:
START TRANSACTION with consistent SNAPSHOT;
The WITH CONSISTENT SNAPSHOT
option starts a consistent read for storage engines that is capable of it. This applies InnoDB
. The effect is the same as issuing a START TRANSACTION
followed by a from any SELECT
InnoDB
table. See section 14.6.3.1.2, "Consistent nonlocking Reads". WITH CONSISTENT SNAPSHOT
the option does the current transaction isolation level, so it provides a consistent snapshot only if the Current isolation level is one that permits a consistent read. The only isolation level, permits a consistent read is REPEATABLE READ
. For all and isolation levels, the WITH CONSISTENT SNAPSHOT
clause is ignored.
Important
Many APIs used for writing MySQL client applications (such as JDBC) provide their own methods for starting transactions th At can (and sometimes should) is used instead of sending a START TRANSACTION
statement from the client. See Chapter, Connectors and APIs, or the documentation for your API, for more information.
To disable autocommit mode explicitly, use the following statement:
SET autocommit=0;
After disabling autocommit mode autocommit
by setting the variable to zero, changes to Transaction-safe tables (such as those F or InnoDB
or NDBCLUSTER
) is not made permanent immediately. You must use to COMMIT
store your changes-to-disk or to ROLLBACK
ignore the changes.
autocommit
is a session variable and must are set for each session. To disable autocommit mode for each new connection, see the description of the autocommit
system variable at section 5.1.4, "Se RVer System Variables ".
BEGIN
and is BEGIN WORK
supported as aliases of START TRANSACTION
initiating a transaction. was standard SQL syntax and is the START TRANSACTION
recommen Ded-to-Start an ad-hoc transaction.
The statement differs from the use of the BEGIN
BEGIN
keyword that starts a BEGIN ... END
compound statement. The latter does not begin a transaction. See section 13.6.1, "BEGIN ... END compound-statement Syntax ".
Note
Within all stored programs (stored procedures and functions, triggers, and events), the parser treats as the BEGIN [WORK]
Beginni Ng of a BEGIN ... END
block. Begin a transaction in the context with START TRANSACTION
instead.
The optional WORK
keyword COMMIT
ROLLBACK
is supported for and, as was the CHAIN
and RELEASE
clauses. CHAIN
and RELEASE
can be us Ed for additional control over transaction completion. The value of the completion_type
system variable determines the default completion behavior. See section 5.1.4, "Server System Variables".
AND CHAIN
the clause causes a new transaction to begin as soon as the current one ends, and the new transaction have the same I Solation level as the just-terminated transaction. The RELEASE
clause causes the server to disconnect, the current client session after terminating the current transaction. Including NO
the keyword suppresses CHAIN
or RELEASE
completion, which can useful if the completion_type
system variable is set To cause chaining or release completion by default.
Beginning a transaction causes any pending transaction to be committed. See sections 13.3.3, "statements that cause a implicit Commit", for more information.
Beginning a transaction also causes table locks acquired LOCK TABLES
with to is released, as though you had executed UNLOCK TABLES
. Beginning a transaction does not release a global read lock acquired with FLUSH TABLES WITH READ LOCK
.
For best results, transactions should is performed using only tables managed by a single Transaction-safe storage engine. Otherwise, the following problems can occur:
If tables from more than one Transaction-safe storage engine (such InnoDB
as), and the transaction isolation level Is isn't SERIALIZABLE
, it is possible this when one transaction commits, another ongoing transaction that uses the same tables would See only some of the changes made by the first transaction. That is, the atomicity of transactions are not guaranteed with mixed engines and inconsistencies can result. (If mixed-engine transactions is infrequent, you can use to set the isolation level to on SET TRANSACTION ISOLATION LEVEL
SERIALIZABLE
a per-transaction b ASIS as necessary.)
If You use tables that is not transaction-safe within a transaction, changes to those tables is stored at once, Regardle SS of the status of Autocommit mode.
If you issue a ROLLBACK
statement after updating a nontransactional table within a transaction, an ER_WARNING_NOT_COMPLETE_ROLLBACK
warning occurs. Changes to Transaction-safe tables is rolled back, and not changes to Nontransaction-safe tables.
Each transaction are stored in the binary log in one chunk, upon COMMIT
. Transactions that is rolled back is not logged. (Exception: Modifications to nontransactional tables cannot is rolled back.) If a transaction that's rolled back includes modifications to nontransactional tables, the entire transaction is logged W ITH a ROLLBACK
statement at the end to ensure, modifications to the nontransactional tables is replicated.) See section 5.2.4, "The Binary Log".
You can change the isolation level to transactions with the SET TRANSACTION
statement. See section 13.3.6, "SET TRANSACTION Syntax".
Rolling back can is a slow operation that may occur implicitly without the user has explicitly asked for it (for Exampl E, when an error occurs). Because of this, SHOW PROCESSLIST
displays Rolling back
State
in the "for the" session, not only for explicit rollbacks performed with The ROLLBACK
statement but also for implicit rollbacks.
START TRANSACTION, COMMIT, and ROLLBACK Syntax-from cyber