5 Advanced Rdbms Concepts 10xii
5 Advanced Rdbms Concepts 10xii
Transaction : A transaction is a unit of work that must be done in logical order and
successfully as a group or not done at all.
START TRANSACTION statement commits the current transaction and starts a new transaction.
It tells MySQL that the transaction is beginning and the statements that follow should be treated
as a unit, until the transaction ends. It is written like this:
START TRANSACTION;
COMMIT Statement:
The COMMIT statement is used to save all changes made to the database during the transaction
to the database. Commit statement is issued at a time when the transaction is complete- all the
changes have been successful and the changes should be saved to the database. COMMIT ends
the current transaction. COMMIT statement is used like this:
COMMIT;
Or
COMMIT WORK;
ROLLBACK Statement:
When a transaction is being executed, some type of error checking is usually performed to
check whether it is executing successfully or not. If not, the entire transaction is undone using
the ROLLBACK statement. The ROLLBACK statement cancels the entire transaction i.e. It rolls
the transaction to the beginning. It aborts any changes made during the transaction and the
state of database is returned to what it was before the transaction began to execute and does
not save any of the changes made to the database during the transaction. ROLLBACK statement
is used like this:
ROLLBACK;
Or
ROLLBACK WORK;
SAVEPOINT:
The SAVEPOINT statement defines a marker in a transaction. These markers are useful in rolling
back a transaction till the marker.
We can add a savepoint anywhere in a transaction. When you roll back to that savepoint, any
changes made to the database after the savepoint are discarded, and any changes made prior
to the savepoint are saved. It is like semicomitting a transaction. To define a savepoint, we
enter the SAVEPOINT statement like this:
SAVEPOINT <savepoint-name>;
Example :
SAVEPOINT Mark1;
In the above statement a marker (savepoint) with the name Mark1 is defined. It becomes a
bookmark in the transaction. Now we can write the following statement:
AUTOCOMMIT:
When AutoCommit is ON, each SQL statement is a transaction. The changes resulting from each
statement are automatically committed.
When Auto Commit is Off then changes made to database are not committed unless explicitly
requested.