Oracle Tips by Burleson
A transaction is a
logically grouped set of INSERTs, UPDATEs and DELETEs that should all
succeed or fail as a group. We just found a new author that has
already written a book. We want to enter the data into our PUBS
database. Because we normalized our schema, we have to enter the data
into three different tables; author,
book_author and book. We want all three inserts to either
succeed or fail as a group; otherwise, we will have some data in some
tables but not a complete record of the new author and his book. We
log onto the database and insert the three rows of data.
2 VALUES ('A11l', 'john',
3 'garmany', '123-345-4567',
4 '1234 here st', 'denver',
5 'CO','90204', '9999');
INTO BOOK_AUTHOR VALUES ('A111', 'B130', .20);
2 VALUES ('B130', 'P002', 'easy oracle sql',
3 'miscellaneous', 9.95, 1000, 15, 0, '',
4 to_date ('02-20-2005','MM-DD-YYYY'));
All three of my SQL statements succeeded, so I
committed the changes. Once I commit the changes, they are
permanently changed in the database and the change cannot be undone.
Let’s say that one of the three inserts failed. Since I don’t want
half the information in the database, I can issue a ROLLBACK command
and the changes since the last commit (or since log on) are removed
from the database and the original state is recreated.
A database transaction is all
the changes that take place between a commit or rollback; beginning
with the first SQL statement and ending with the subsequent commit or
rollback. A commit can be issued as a command or happen because of
another statement. Anytime we execute a data definition language
(DDL) command, we issue an implicit commit. In the example
below, I INSERT a duplicate row with the author key A111. I then create
a table (DDL) and rollback.
into book_author values ('A111', 'B130', .20);
table t1 as
4 avg(quantity) qty
5 6 store join sales using (store_key)
7 group by store_name;
author_key from book_author;
Notice that the duplicate row remains after the
rollback. That is because I created a table that issued an implicit
commit. The duplicate row was committed by the CREATE TABLE command.
Before a commit or rollback is executed, I have
the ability to recover any data changes in the transaction. If
I select data that I have changed but not committed, I will get back
the changed data. If another user selects data that I have changed
but not committed, they will see the original data (a consistent view
will be discussed next).
A commit is issued when the user commits, when any
DDL statement is executed, or when the user logs off
normally. If you make a number of changes and you do not want the
changes to be permanent, you must issue a rollback before you log
off. Logging off commits the changes.
The above book excerpt is from:
Fast writing SQL Reports with SQL*Plus