Database Trigger Analysis (dtrig.sql)
What This Script Does
Section titled “What This Script Does”This script analyzes database triggers by:
- Showing all triggers for specified owners and tables
- Displaying trigger types and firing events
- Indicating disabled triggers (enabled triggers show no status)
- Grouping output by table owner and name
- Supporting pattern matching for flexible filtering
Script
Section titled “Script”rem dtrig.sqlremttitle 'Database Triggers'remcol table_owner format a8 heading 'TABLE|OWNER'col table_name format a30 heading 'TABLE NAME'col trigger_name format a30 heading 'TRIGGER NAME'col status format a8 heading 'STATUS'col trigger_type format a16 heading 'TRIGGER TYPE'col triggering_event format a26 heading 'TRIGGERING EVENT'col owner format a8 heading 'TRIGGER|OWNER'rembreak on table_owner on table_nameremselect table_owner, table_name, trigger_name, decode( status, 'ENABLED', null, status ) status, trigger_type, triggering_event, owner from sys.dba_triggers where table_owner like upper('&owner') and table_name like upper('&table') order by table_owner, table_name, trigger_name/
SQL> @dtrig.sqlEnter value for owner: HREnter value for table: EMPLOYEES
Parameters
Section titled “Parameters”- owner: Table owner pattern to filter (use % for all)
- table: Table name pattern to filter (use % for all)
Required Privileges
Section titled “Required Privileges”- SELECT on SYS.DBA_TRIGGERS
Sample Output
Section titled “Sample Output”Database Triggers
TABLE TABLE NAME TRIGGER NAME STATUS TRIGGER TYPE TRIGGERING EVENT TRIGGEROWNER OWNER-------- ------------------------------ ------------------------------ -------- ---------------- -------------------------- --------HR EMPLOYEES EMP_AUDIT_TRG AFTER EACH ROW INSERT OR UPDATE OR DELETE HR EMPLOYEES EMP_ID_TRG BEFORE EACH ROW INSERT HR EMPLOYEES EMP_SALARY_CHECK DISABLED BEFORE EACH ROW UPDATE HR
DEPARTMENTS DEPT_AUDIT_TRG AFTER EACH ROW INSERT OR UPDATE OR DELETE HR DEPARTMENTS DEPT_CODE_TRG BEFORE EACH ROW INSERT OR UPDATE HR
SCOTT EMP EMP_INSERT_TRG BEFORE EACH ROW INSERT SCOTT DEPT DEPT_DELETE_RESTRICT BEFORE STATEMENT DELETE SCOTT
Key Output Columns
Section titled “Key Output Columns”- TABLE OWNER: Schema that owns the table
- TABLE NAME: Name of the table with triggers
- TRIGGER NAME: Name of the trigger
- STATUS: Shows DISABLED if trigger is disabled (blank = enabled)
- TRIGGER TYPE: When and how the trigger fires
- TRIGGERING EVENT: Events that cause trigger to fire
- TRIGGER OWNER: Schema that owns the trigger
Understanding Trigger Types
Section titled “Understanding Trigger Types”Timing Types
Section titled “Timing Types”- BEFORE: Fires before the triggering event
- AFTER: Fires after the triggering event
- INSTEAD OF: Fires instead of triggering event (views only)
Granularity Types
Section titled “Granularity Types”- STATEMENT: Fires once per SQL statement
- ROW: Fires once per affected row
Common Combinations
Section titled “Common Combinations”- BEFORE EACH ROW: Row-level validation, value modification
- AFTER EACH ROW: Auditing, logging, cascading updates
- BEFORE STATEMENT: Statement-level validation
- AFTER STATEMENT: Cleanup, summary operations
Triggering Events
Section titled “Triggering Events”DML Events
Section titled “DML Events”- INSERT: New row creation
- UPDATE: Row modification
- DELETE: Row removal
- INSERT OR UPDATE: Multiple events
- INSERT OR UPDATE OR DELETE: All DML operations
DDL Events (Database triggers)
Section titled “DDL Events (Database triggers)”- CREATE: Object creation
- ALTER: Object modification
- DROP: Object removal
System Events
Section titled “System Events”- STARTUP: Database startup
- SHUTDOWN: Database shutdown
- LOGON: User session start
- LOGOFF: User session end
Common Use Cases
Section titled “Common Use Cases”-
Audit Analysis
- Find audit triggers on sensitive tables
- Verify trigger coverage
- Check audit trail completeness
-
Performance Investigation
- Identify triggers affecting DML performance
- Find complex trigger logic
- Analyze trigger overhead
-
Security Review
- Validate security triggers
- Check data protection mechanisms
- Audit trigger privileges
-
Maintenance Planning
- Find disabled triggers
- Identify triggers needing updates
- Plan trigger modifications
Advanced Analysis
Section titled “Advanced Analysis”Find disabled triggers:
Section titled “Find disabled triggers:”SELECT owner, table_name, trigger_name, statusFROM dba_triggersWHERE status = 'DISABLED'ORDER BY owner, table_name;
Show trigger code:
Section titled “Show trigger code:”SELECT trigger_name, trigger_bodyFROM dba_triggersWHERE table_name = 'EMPLOYEES'AND table_owner = 'HR';
Count triggers by table:
Section titled “Count triggers by table:”SELECT table_owner, table_name, COUNT(*) trigger_countFROM dba_triggersWHERE table_owner NOT IN ('SYS','SYSTEM')GROUP BY table_owner, table_nameHAVING COUNT(*) > 3ORDER BY trigger_count DESC;
Trigger Management
Section titled “Trigger Management”Enable/Disable triggers:
Section titled “Enable/Disable triggers:”-- Disable triggerALTER TRIGGER hr.emp_audit_trg DISABLE;
-- Enable triggerALTER TRIGGER hr.emp_audit_trg ENABLE;
-- Disable all triggers on tableALTER TABLE hr.employees DISABLE ALL TRIGGERS;
-- Enable all triggers on tableALTER TABLE hr.employees ENABLE ALL TRIGGERS;
Trigger Dependencies:
Section titled “Trigger Dependencies:”SELECT name, referenced_name, referenced_typeFROM dba_dependenciesWHERE name = 'EMP_AUDIT_TRG'AND owner = 'HR';
Performance Considerations
Section titled “Performance Considerations”Trigger Overhead
Section titled “Trigger Overhead”- Row-level triggers fire for each affected row
- Complex trigger logic impacts DML performance
- Multiple triggers compound overhead
- Cascading triggers can cause recursion
Best Practices
Section titled “Best Practices”- Keep trigger logic simple
- Avoid heavy processing in triggers
- Use statement-level triggers when possible
- Consider alternative approaches (procedures, jobs)
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”- Mutating table errors: Triggers accessing triggering table
- Cascading effects: Triggers firing other triggers
- Performance degradation: Complex trigger logic
- Constraint violations: Trigger logic conflicts
Investigation Steps
Section titled “Investigation Steps”- Check trigger status and logic
- Review error logs and trace files
- Analyze execution plans
- Test with triggers disabled