Skip to Main Content
  • Questions
  • Instead of triggers: Good or bad solution?

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Mathieu.

Asked: June 25, 2008 - 2:46 am UTC

Last updated: June 26, 2008 - 3:43 pm UTC

Version: 10.2

Viewed 1000+ times

You Asked

Hello Tom,

A while ago we had a discussion on your blog about instead-of-triggers. As I understand you aren't very fond of it. What would you do in the following situation?

There is an existing application (tables and business rules are stored in a schema on the database). All kind of interfaces are connected to this database (batches, Oracle forms (client-server), etc...).

Now it is decided that they want to join the new world. So the Oracle forms will be replaced by a webbased interface (something like ruby, hibernate or java, etc...). The database is kept of course. The new interface is a framework that requires a certain format for the tablestructure. For instance the primary key must be called ID. However the existing tables have a different setup. Say the primary key is called EMP_ID.
In my opinion I would not change the existing tables, because existing interfaces should still work.

So it was decided to use views on the existing tables which transforms the table format in the required tableformat of the framework. To deal with the insert/update/delete instead-of-triggers, which calls packages to handle the insert/update/delete, were used. Keep the trigger as simple as possible. All the coding is done in the package.

I think this a efficient way to link a framework with an existing databaseschema without having to change either of them.

As you said you aren't fond of using instead-of-triggers, how you deal with this type of situation in general?

Thanks.

and Tom said...

I would look at other frameworks that do not dictate a database schema structure.

Seriously.

100%

I would not even consider a framework that said "you must have this thing called ID"

How very very lame is that?

Who makes such a decision? It surely isn't anyone that cares about data (and face it, without good data - it doesn't matter how cool the 'framework' is). An application framework should not be the driving factor in a data model - the requirements of the business are.

Going down the path you are will be nightmarish. The resulting system (this is not "probably", this is assured) will be understood by NO ONE over time. The developers will say "but I just did a single row update" - yeah but in the background there was 500 lines of code executing. Side effect upon side effect.

Sorry - you will never get me to say this is a good or even a reasonable idea. It is a hack.


Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

lifetime dancing lessons?

Duke Ganote, June 25, 2008 - 9:46 am UTC

Someone's way too Java-centric. However, if they're willing to pay for Oracle developers to build and maintain a cocoon for them to 'hibernate' in...well, sounds like job security to me!

Hibernate? UGGHH

Tom, June 25, 2008 - 1:52 pm UTC

I had to deal with app developers switching to Hibernate about 2 years back... I still remember the discussions about how great it is (or is not)... and I remember the Formal namespacing dictates of using ID as the PK column name, i.e : Employee.ID instead of Employee.Emp_ID... that alone is not too bad, but all of the other shortcomings, including the crappy SQL it generated from the OR mapping was enough to make me puke. And the best of all was my manager telling me that we weren't allowed to use PL/SQL for ANYTHING since it wasn't "standards based" like Java and Hibernate...

hiberation

Duke Ganote, June 25, 2008 - 4:07 pm UTC

Sounds like Tom from Gainesville VA is glad to see Hibernate in the rearview mirror.

Lots of comments on Hibernate can be found here
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:224022800346940201

Views without instead of triggers

Gary, June 25, 2008 - 8:55 pm UTC

If it is PURELY column naming standards, views can work perfectly well without an INSTEAD OF trigger.

create table good_name (good_col_1 number, good_col_2 varchar2(10));

create view std_name (id, std_col_1)
as select good_col_1, good_col_2 from good_name;

insert into std_name (id, std_col_1)
values (1,'test');

select * from this_is_a_good_name;


Tom Kyte
June 26, 2008 - 3:43 pm UTC

true, a view is an excellent way to just "rename" a column

however, I presumed there must have been a reason to use 'code' with instead of inserts. I think they are trying to keep the old system "as it was" and the new framework says "nope, we don't do it that way" and the instead of insert triggers are there to trick the framework into thinking it is actually doing something.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library