DWDW

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Database Programming with

PL/SQL
3-4
Using Transaction Control Statements

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.


Objectives

This lesson covers the following objectives:


• Define a transaction and provide an example
• Construct and execute a transaction control statement in
PL/SQL
• Since Oracle Application Express automatically commits
changes, the following information will be presented as if you
were issuing the commands in an installed/local environment
with the ability to use COMMIT and ROLLBACK.

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3
Using Transaction Control Statements
Purpose

• In this lesson, you learn how to include transaction control


statements such as COMMIT, ROLLBACK, and SAVEPOINT in
PL/SQL.
• Just think, if you write a paper for your teacher in pencil, you
have captured your thoughts but not yet turned the paper in.
If you want to change something on your paper, you can use
an eraser to make the change.

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4
Using Transaction Control Statements
Purpose
• But once you turn the paper in to your teacher, you have
completed the task. Transaction control statements in PL/SQL
allow you to do the same thing using the keywords COMMIT,
ROLLBACK, and SAVEPOINT.

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5
Using Transaction Control Statements
Database Transaction

• A transaction is an inseparable list of database operations


that must be executed either in its entirety or not at all.
Transactions maintain data integrity and guarantee that the
database is always in a consistent state.

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6
Using Transaction Control Statements
Example of a Transaction

• To illustrate the concept of a transaction, consider a banking


database. When a bank customer transfers money from a
savings account to a checking account, the transaction can
consist of three separate operations:

Decrease savings Increase checking Record the


account account transaction in the
balance. balance. transaction journal.

Transaction

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 7
Using Transaction Control Statements
Example of a Transaction
• What would happen if there were insufficient funds in the
savings account? Would the funds still be added to the
checking account? Would an entry be logged in the
transaction journal? What do you think should happen?

Decrease savings UPDATE savings_accounts


account SET balance = balance - 500
balance. WHERE account = 3209;

Increase checking UPDATE checking_accounts


account SET balance = balance + 500
balance. WHERE account = 3208;

Record the transaction INSERT INTO journal VALUES


in the transaction (journal_seq.NEXTVAL, '1B'
journal. 3209, 3208, 500);

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 8
Using Transaction Control Statements
Example of a Transaction
• If all three SQL statements can be performed to maintain the
accounts in proper balance, the effects of the transaction can
be committed, or applied to the database tables.

If OK, then
Decrease savings
account.

Increase checking If OK, then


account.

Record the
transaction in the If OK, then
transaction journal. COMMIT!

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 9
Using Transaction Control Statements
Example of a Transaction
• However, if a problem, such as insufficient funds, invalid
account number, or a hardware failure prevents one or two of
the statements in the transaction from completing, the entire
transaction must be rolled back (reversed out) so that the
balance of all accounts is correct.

If OK, then
Decrease savings
account.

Increase checking If not OK then


ROLLBACK!
account.
Record the
transaction in the
transaction journal.

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 10
Using Transaction Control Statements
Transaction Control Statements
• You use transaction control statements to make the changes
to the database permanent or to discard them. The three
main transaction control statements are:
– COMMIT
– ROLLBACK
– SAVEPOINT

• The transaction control commands are valid in PL/SQL and


therefore can be used directly in the executable or exception
section of a PL/SQL block.

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 11
Using Transaction Control Statements
COMMIT

• COMMIT is used to make the database changes permanent. If


a transaction ends with a COMMIT statement, all the changes
made to the database during that transaction are made
permanent.
BEGIN
INSERT INTO pairtable VALUES (1, 2);
COMMIT;
END;

• Note: The keyword END signals the end of a PL/SQL block, not
the end of a transaction.

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 12
Using Transaction Control Statements
ROLLBACK

• ROLLBACK is for discarding any changes that were made to


the database after the last COMMIT. If the transaction fails, or
ends with a ROLLBACK, then none of the statements takes
effect.
• In the example, only the second INSERT statement adds a row
of data.

BEGIN
INSERT INTO pairtable VALUES (3, 4);
ROLLBACK;
INSERT INTO pairtable VALUES (5, 6);
COMMIT;
END;

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 13
Using Transaction Control Statements
SAVEPOINT

• SAVEPOINT is used to mark an intermediate point in


transaction processing. Only ROLLBACK can be used to a
SAVEPOINT.

BEGIN
INSERT INTO pairtable VALUES (7, 8);
SAVEPOINT my_sp_1;
INSERT INTO pairtable VALUES (9, 10);
SAVEPOINT my_sp_2;
INSERT INTO pairtable VALUES (11, 12);
ROLLBACK to my_sp_1;
INSERT INTO pairtable VALUES (13, 14);
COMMIT;
END;

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 14
Using Transaction Control Statements
Terminology

Key terms used in this lesson included:


• COMMIT
• END
• ROLLBACK
• SAVEPOINT
• Transaction

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 15
Using Transaction Control Statements
Summary

In this lesson, you should have learned how to:


• Define a transaction and provide an example
• Construct and execute a transaction control statement in
PL/SQL

PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 16
Using Transaction Control Statements

You might also like