PLSQL 13 5
PLSQL 13 5
PLSQL 13 5
PL/SQL
13-5
Managing Triggers
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3
Managing Triggers
Purpose
• There may be times when you want to turn off a trigger
in order to perform some maintenance or debug some
code.
• Or, in order to understand the triggers that exist in the
Data Dictionary, you may need to view them.
• You can do all of this by managing triggers.
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 4
Managing Triggers
Privileges Needed for Triggers
• To create a trigger in your own schema, you need:
– CREATE TRIGGER system privilege
– Normal object privileges (SELECT, UPDATE, EXECUTE, and so
on) on objects in other schemas that are referenced in your
trigger body
– ALTER privilege on the table or view associated with the trigger.
• To create triggers in other users' schemas, you need the
CREATE ANY TRIGGER privilege.
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 5
Managing Triggers
Privileges Needed for Triggers
• Statements in the trigger body use the privileges of the
trigger owner (Definer's Rights), NOT the privileges of
the user executing the operation that fires the trigger
(Invoker's Rights).
• You cannot specify Invoker’s Rights (AUTHID
CURRENT_USER) for a trigger.
• The next slide shows an example.
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 6
Managing Triggers
Privileges Needed for Triggers Example
• User Monica needs to create the following trigger:
CREATE OR REPLACE TRIGGER upd_tom_emp
AFTER UPDATE ON tom.employees
BEGIN
INSERT INTO mary.log_table VALUES(USER, SYSDATE);
sharon.calledproc;
END;
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 7
Managing Triggers
Viewing Triggers in the Data Dictionary
You can see trigger information in the following Data
Dictionary views:
• USER_OBJECTS: Object name and object type (as for all
other object types in your schema)
• USER_TRIGGERS: Detailed code and status of the trigger
• USER_ERRORS: PL/SQL syntax errors (compilation
errors) of the trigger
• Source code for triggers is in USER_TRIGGERS not
USER_SOURCE.
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 8
Managing Triggers
USER_TRIGGERS Data Dictionary
Column* Column Description
TRIGGER_NAME Name of the trigger
TRIGGER_TYPE When it fires - BEFORE, AFTER, ROW, etc.
TRIGGERING_EVENT The DML operation firing the trigger
TABLE_NAME Name of the associated table
REFERENCING_NAMES Name used for :OLD and :NEW
WHEN_CLAUSE The when_clause used
STATUS The status of the trigger
TRIGGER_BODY Action taken by the trigger
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 9
Managing Triggers
Viewing Trigger Information Using
USER_TRIGGERS
• This example shows the triggering event, timing, type of
trigger, status, and detailed body code of the
RESTRICT_SALARY trigger:
SELECT trigger_name, trigger_type, triggering_event,
table_name, status, trigger_body
FROM USER_TRIGGERS
WHERE trigger_name = 'RESTRICT_SALARY';
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 10
Managing Triggers
Changing the Status of Triggers
• If you need a trigger turned off temporarily, don't drop
it and then recreate it, just disable it for a little while by
using the ALTER TRIGGER statement.
• Disable or re-enable a database trigger:
ALTER TRIGGER trigger_name DISABLE | ENABLE;
• Recompile a trigger
ALTER TRIGGER for aCOMPILE;
trigger_name table:
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 12
Managing Triggers
Removing Triggers
• To remove a trigger from the database, use the DROP
TRIGGER statement:
DROP TRIGGER trigger_name;
• Example:
DROP TRIGGER secure_emp;
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 13
Managing Triggers
Terminology
Key terms used in this lesson included:
• ALL_TRIGGERS
• USER_TRIGGERS
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 14
Managing Triggers
Summary
In this lesson, you should have learned how to:
• View trigger information in the Data Dictionary
• Disable and enable a database trigger
• Remove a trigger from the database
PLSQL S13L5 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 15
Managing Triggers