5

I have a trigger to check for a single column in a table, when there is an update (AFTER UPDATE) in that column my trigger is called and then I call a stored procedure from within my trigger in order to perform some business logic coded in Java.

So far, so good. Now things are getting more complicated, there is a new requirement that implies that the same logic (the logic executed by the trigger) should be also be performed if there are changes in columns that are in other 4 tables. I think it's not practical to have the same trigger in 5 different tables listening for different columns, and I'm not sure if I should consider creating a "view" for these columns and put a single trigger in that view (what is the cost to pay in terms of performance and/or resources?)

In your previous experience with Oracle, what have been your approach or solutions for this scenario?

3 Answers 3

5

The only way you can use one trigger for multiple tables is to create view.

If I didn't want to create view, I'd create a script(or whatever, use the language you want) and a list of tables on which this trigger to be created (make the creating of triggers dynamic).. I see your point is to have best maintainability on this situation and I think creating a view is cleaner solution(and easy to maintain)than dynamic creating of a trigger for each table.

1
  • We finally decided to use an approach using a view for certain purposes but we had to keep a couple of triggers in the design for some specific purposes. Your answer is the most similar thing to what we decided to do, but the other suggestions were great and were actually also considered. Thanks Svetlozar! Aug 31, 2009 at 22:11
2
  1. Can you actually create a view joining all the tables in question? Do they have foreign keys to each other?
  2. Assuming you can create a view, will all tables be updatable using the complex view? There are severe limitations on this.
  3. Generally, we create only INSTEAD OF triggers on a view. There are also restrictions on triggers based on UPDATE operations. For a complete list of the issues involved refer to

http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_7004.htm#SQLRF01405

Note: I am assuming ORACLE is the db as you have tagged your question with ORACLE

2
  • Yes, I'm using Oracle. I can join all these columns in a single view, but I'm still unsure if I'll be able to update this complex view, so I'm thinking that the INSTEAD OF approach will work for me. I'll try that approach and then I'll let you know what happened. Aug 27, 2009 at 20:10
  • Thanks bkm, we were just about to implement this approach (INSTEAD OF), but we faced some technical limitations and constraints that we couldn't overcome. Aug 31, 2009 at 22:12
2

Why not have your logic in a stored procedure, then just call that from the triggers?

I'd also suggest you take look at change notification, from that sound of it, thats probably what you want more so than just a plain old trigger.

Also be careful when calling external resources. Anything you call in a trigger should be transactional, as Oracle may run the trigger several times before your DML is actually completed.

1
  • Thanks specially for the reminder about transactional operations within triggers, that helped me to point a problem with a candidate solution. Thanks again! Aug 31, 2009 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.