How bad is it to simulate IDENTITY/AUTOINCREMENT columns using triggers in Oracle?
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 t开发者_JS百科his 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?
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!
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.
精彩评论