0% found this document useful (0 votes)
7 views

SQL - TRANSACTIONS & TCL Statements

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQL - TRANSACTIONS & TCL Statements

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

DBS211

STRUCTURED QUERY LANGUAGE (SQL)

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.

• A transaction terminates when one of the following occurs:


• A COMMIT or ROLLBACK statement is issued.
• A DDL statement, such as CREATE, is issued.
• A DCL statement is issued.
• A machine fails or the system crashes.
• A DISCONNECT from the system
3
Set Transaction
• SET TRANSACTION is used to set the current transaction as read-only or
read/write.

• SET TRANSACTION Clauses


• READ ONLY clause – Transaction Level Read Consistency
• Retrieves only changes that were committed before start of transaction
• READ WRITE clause – Statement-Level Read Consistency
• cannot toggle between Statement-level and Transaction Level
• NAME clause – Assigns a name to the Transaction

SET TRANSACTION READ WRITE NAME 'rw_example’;


4
Controlling Transactions

• Control the logic of transactions by using the COMMIT, SAVEPOINT,


and ROLLBACK

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.

• An automatic rollback occurs under an abnormal termination of


SQLDeveloper or a system failure.

• The AUTOCOMMIT command can be toggled ON or OFF.


• ON: each individual DML statement is committed as soon as it is executed.
• OFF: COMMIT can be issued explicitly. 6
AUTOCOMMIT
• A SQL*PLUS Command.
• Automatically a COMMIT Statement is sent to the system after each DML
statement.

• 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

• Every data change is temporary until the transaction is committed.

• Data manipulation operations primarily affect the database buffer.

• 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.

DELETE FROM employees;

ROLLBACK;

10
Rolling back changes to a Marker
• Create a marker in a current transaction by using the SAVEPOINT statement.

• ROLLBACK to that marker by using the ROLLBACK TO 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

• If a single DML statement fails during execution, only that statement


is rolled back.

• All other changes are retained.

• The user should terminate transactions explicitly by executing a


COMMIT or ROLLBACK statement.

12
Exception Handling
• Catch All Exception Handling

SET SERVEROUTPUT ON;


BEGIN
INSERT INTO employee VALUES (1, 'John','Doe', 50000);
INSERT INTO employee VALUES (2, 'Mike','Doe', 7500000000);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Transaction Commited');
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE ('Transaction rolled back: ' || SQLCODE || ' - ' || sqlerrm);
END;
13
/

You might also like