SQL - TRANSACTIONS & TCL Statements
SQL - TRANSACTIONS & TCL Statements
1
Database Transactions
• Transaction is a logical unit of work that contains one/many SQL Statements.
• ACID Properties
• Atomicity – All SQL Statements are committed or none
• Consistency – Moving from one consistent state to another consistent state
• Isolation - Preview data changes before making changes permanent
• Durability – Committed Transactions are permanent
TYPE DESCRIPTION
Data manipulation language (DML) Consists of any number of DML statements
that the Oracle Server treats as a single entity
or a logical unit of work
Data definition language (DDL) Consists of only one DDL statement
Data control language (DCL) Consists of only one DCL statement
2
When does a Transaction starts and ends?
• A transaction begins when:
• A DML statement is issued.
• A Transaction is started with SET TRANSACTION or BEGIN statement.
• A COMMIT or a DDL statement is issued. Current Transaction is committed, and a new
Transaction is started.
• Open a new SQL Worksheet in SQL Developer.
Statement Description
COMMIT Ends the current transaction by making all pending data changes permanent.
SAVEPOINT name Marks a savepoint within the current transaction
ROLLBACK [ TO ROLLBACK ends the current transaction by discarding all pending data
SAVEPOINT name] changes; ROLLBACK TO SAVEPOINT rolls back the current transaction
to the specified savepoint, thereby discarding the savepoint and any
subsequent changes. If you omit this clause, the ROLLBACK statement
rolls back the entire transaction.
5
Implicit Transaction Processing
• An automatic commit occurs under the following circumstances:
• DDL statement is issued.
• DCL statement is issued.
• DISCONNECT statement is issued.
• Turn on Autocommit
SET AUTOCOMMIT ON;
• Check if AUTOCOMMIT is on/off
Show AUTOCOMMIT;
• Turn off Autocommit
SET AUTOCOMMIT OFF;
7
State of data before COMMIT or ROLLBACK
• The current user can review the results of the data manipulation by using SELECT
statements.
• Other users can not review the changes that current user has made to the database.
• The affected rows are locked; other users can not change the data in the affected rows.
8
State of data after COMMIT
• Make all pending changes permanent by using the COMMIT statement.
• Data changes are written to the database.
• The previous state of data is permanently lost.
• All users can view the results of the transaction.
• The locks are released.
• All savepoints are erased.
UPDATE employees
SET officeCode = 4
WHERE employeeNumber = 1088;
COMMIT; 9
State of data after ROLLBACK
• Discard all pending changes by using the ROLLBACK.
• Data changes are undone.
• The previous state of the data is restored.
• The locks on the affected rows are released.
ROLLBACK;
10
Rolling back changes to a Marker
• Create a marker in a current transaction by using the SAVEPOINT statement.
• If you create a second savepoint with the same name as an earlier savepoint, the earlier savepoint
is deleted.
UPDATE ………..
SAVEPOINT update_done;
INSERT …………
ROLLBACK TO update_done;
11
Statement-Level Rollback
12
Exception Handling
• Catch All Exception Handling