Oracle 3 Transactions Analytic Functions
Oracle 3 Transactions Analytic Functions
Lecture 3
Transactions
SQL Language: Additional information
SQL Language: Analytic Functions
Database transactions
Every operation in Oracle is executed in a
transaction
Transactions are started automatically when
user executes first statement:
after opening new database session
after ending previous transaction
Transactions
Transaction can end with:
COMMIT – changes are saved in the database
ROLLBACK – changes are canceled and removed
from the database
Transaction ends automatically when:
user properly disconnects from the database
(transaction is committed)
database crashes, or user session disconnects
improperly (transaction is rolled back)
Transactions and sessions
Separate sessions created by the same user
work in separate transactions
Transaction is always opened by some
database session
single session usually has only one open
transaction (exception: autonomous transactions)
once transaction is committed or rolled back,
session opens new transaction
Transactions and savepoints
Savepoint saves current state of a transaction
It is possible to roll back to a savepoint (partial
rollback of a transaction)
Example:
delete from test;
savepoint s1;
insert into test(id) values (1);
rollback to s1;
commit; -- table test is empty
The ACID model
Database transactions should follow the ACID
model
A – Atomicity
C – Consistency
I – Isolation
D – Durability
Atomicity
Database transaction should be atomic:
entire transaction should be treated as a single
operation
other sessions should not see results of
uncommitted transactions
if a database crashes or session disconnects,
partial (uncommitted) transactions should not be
visible, they should automatically be rolled back
Consistency
Consistency states that only valid data will be
written to the database
Transactions violating consistency rules
(constraints) will be rolled back
Isolation
Multiple transactions should be isolated from
each other
If two transactions are issued at the same time,
one should execute before another, so that they
do not interfere with each other
In practice: single transaction should not see
results of another transactions executing at the
same time
Durability
Transaction once committed can never be lost
Durability is ensured through database backups
and redo logs
Transaction concurrency
Typical problems related to concurrency:
dirty read - transaction reads data that is not yet
committed (violates atomicity and isolation)
non repeatable read - transaction reads the same
data twice and sees different results, because it
was modified and committed by another transaction
(violates isolation)
phantoms - transaction executes the same query
twice and in the second execution there are
additional rows that were inserted and committed in
the mean time (violates isolation)
Isolation levels
Standard isolation levels:
READ UNCOMMITTED - transactions see
uncommitted data from other transactions, dirty
read, non repeatable read and phantoms are
possible
READ COMMITTED - transactions see committed
data, phantoms and unrepeatable reads are
possible.
REPEATABLE READ - the same query executed
twice gives the same results with the exception of
phantoms.
SERIALIZABLE - transactions are fully isolated.
Oracle's isolation levels
Oracle supports the following isolation levels:
READ COMMITTED – default isolation level
SERIALIZABLE – can be activated with:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
READ ONLY – non standard level, can be activated
with:
SET TRANSACTION READ ONLY
Isolation level can only be set as the first
statement in the transaction
If set – isolation level remains until the end of
the transaction
SQL Language
NULL values in SQL
NULL value has special “no value” meaning
NULL compared with any other value results in
NULL:
SELECT * FROM users WHERE login = NULL
SELECT * FROM users WHERE login != NULL
Both statements return 0 rows
Logical condition can have 3 values:
TRUE
FALSE
NULL (UNKNOWN)
NULL logical conditions
Example:
INSERT INTO users (login, name) VALUES (NULL, 'Some
user');
INSERT INTO users (login, name)
VALUES ('user1', 'Some other user');
SELECT * FROM (
SELECT e.name, e.salary, e.dept_id,
ROW_NUMBER() OVER (PARTITION BY dept_id
ORDER BY salary DESC) rnum
FROM emp e)
WHERE rnum <= 3;