05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

280<br />

CHAPTER 8 ■ TRANSACTIONS<br />

So, we have set up the EMP table that has a nice hierarchical structure (EMPNO, MGR recursive<br />

relationship). We also have an AUDIT_TAB table into which we want to record failed attempts to<br />

modify information. We have a trigger to enforce our rule that only our manager or our manager’s<br />

manager (<strong>and</strong> so on) may modify our record.<br />

Let’s see how this works by trying to update a record in the EMP table:<br />

ops$tkyte@ORA10G> update emp set sal = sal*10;<br />

update emp set sal = sal*10<br />

*<br />

ERROR at line 1:<br />

ORA-20001: Access Denied<br />

ORA-06512: at "OPS$TKYTE.EMP_AUDIT", line 21<br />

ORA-04088: error during execution of trigger 'OPS$TKYTE.EMP_AUDIT'<br />

ops$tkyte@ORA10G> select * from audit_tab;<br />

USERNAME TIMESTAMP MSG<br />

--------- --------- ----------------------------------------<br />

OPS$TKYTE 27-APR-05 Attempt to update 7369<br />

The trigger caught us <strong>and</strong> was able to prevent the UPDATE from occurring, while at the<br />

same time creating a permanent record of the attempt (notice how it used the DEFAULT keyword<br />

on the CREATE TABLE statement for the AUDIT_TAB table to automatically have the USER<br />

<strong>and</strong> SYSDATE values inserted for us). Next, let’s log in as a user who can actually do an<br />

UPDATE <strong>and</strong> try some things out:<br />

ops$tkyte@ORA10G> connect scott/tiger<br />

Connected.<br />

scott@ORA10G> set echo on<br />

scott@ORA10G> update ops$tkyte.emp set sal = sal*1.05 where ename = 'ADAMS';<br />

1 row updated.<br />

scott@ORA10G> update ops$tkyte.emp set sal = sal*1.05 where ename = 'SCOTT';<br />

update ops$tkyte.emp set sal = sal*1.05 where ename = 'SCOTT'<br />

*<br />

ERROR at line 1:<br />

ORA-20001: Access Denied<br />

ORA-06512: at "OPS$TKYTE.EMP_AUDIT", line 21<br />

ORA-04088: error during execution of trigger 'OPS$TKYTE.EMP_AUDIT'<br />

In the default install of the demonstration table EMP, the employee ADAMS works for SCOTT,<br />

so the first UPDATE succeeds. The second UPDATE, where SCOTT tries to give himself a raise, fails<br />

since SCOTT does not report to SCOTT. Logging back into the schema that holds the AUDIT_TAB<br />

table, we see the following:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!