Explain Codes LogoExplain Codes Logo

Oracle: SQL query to find all the triggers belonging to the tables?

sql
trigger-management
sql-queries
database-administration
Anton ShumikhinbyAnton Shumikhin·Nov 21, 2024
TLDR

To unearth all the triggers associated with the tables in your Oracle schema, conduct a quick investigation on the ALL_TRIGGERS view:

SELECT trigger_name, table_name, status FROM all_triggers WHERE owner = 'YOUR_SCHEMA'; -- replace YOUR_SCHEMA with actual schema. Don't be that guy.

Swap 'YOUR_SCHEMA' with your schema name. This command disgorges the trigger names, their associated tables, and the statuses echoing whether they are enabled or disabled.

Do you not enjoy the privileges to cross-question ALL_TRIGGERS? Worry not! You can chit-chat with DBA_TRIGGERS with your DBA privileges or get insider info from USER_TRIGGERS to catalogue triggers spawned by the current user.

Unlocking multiple triggers and diverse schemas

Ordering multiple triggers

Fancy a table with multiple triggers? Be Sherlock. Analyze the order of firing the triggers. Remember, they follow a sportsmanship spirit when they belong to the same timing/event combination (BEFORE UPDATE, for instance). Your ally here - trigger_order column from the ALL_TRIGGERS view.

  • Peeping into another's schema: SELECT * FROM all_triggers WHERE table_owner = 'ANOTHER_SCHEMA'; Not spying, just browsing!
  • Self-introspection: SELECT * FROM user_triggers; See the triggers answering to user you.

Super-efficient trigger management

Opt for these queries for peak performance in handling triggers:

  • Status-check: Regular patrol to ensure triggers are enabled or disabled as per your wish.
  • Event attendance: Stay aware of the triggers' presence at the triggering event (INSERT, UPDATE, DELETE, TRUNCATE). Find them using SELECT triggering_event FROM all_triggers;.
  • Timing specs: SELECT trigger_type FROM all_triggers; precisely tells you the timing of the triggers (BEFORE, AFTER, INSTEAD OF).

Finding triggers sprawled across schemas

Unearth triggers around all schemas:

SELECT OWNER || '.' || TRIGGER_NAME as FULL_TRIGGER_NAME, -- Schema ownership TABLE_NAME FROM ALL_TRIGGERS;

Avail a full identifier that seamlessly blends the schema and trigger name.

References