views:

347

answers:

2

I maintain an application that was originally written to be SQL Server-specific (using IDENTITY fields). Thus, we've had to define a lot of triggers to auto increment tables' primary keys.

I'm told that this is considered to be a hacky workaround in the Oracle world, but that was told to me in a "friend of a friend" kind of way. How big a deal is it to use triggers to increment primary keys from a sequence instead of using the sequence directly?

+8  A: 

It is a very common practice in my experience, and not a terribly bad one. However, if you have control over the inserts (e.g. if all inserts are done via a PL/SQL API) then it is more efficient to use the sequence directly in the INSERT statement - because it avoids the overhead of firing a trigger. But I really wouldn't worry unduly about it if you have used triggers!

Tony Andrews
That's about what I was thinking. I figured it was just considered "good practice" in much the same manner as closing files instead of not letting them be closed at process end.
Jason Baker
+1, your options are pretty limited.
DCookie
+1  A: 

This may not be totally relevant, but using "before insert trigger" to auto-increment primary keys is a good option especially in Merge statements. If a sequence is used directly inside Merge insert section, the sequence seems to be invoked even for updates.

Dinesh Bhat