DWDW
DWDW
DWDW
PL/SQL
3-4
Using Transaction Control Statements
PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3
Using Transaction Control Statements
Purpose
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
PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6
Using Transaction Control Statements
Example of a Transaction
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?
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.
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.
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
PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 11
Using Transaction Control Statements
COMMIT
• 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
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
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
PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 15
Using Transaction Control Statements
Summary
PLSQL 3-4 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 16
Using Transaction Control Statements