在 Oracle 中使用触发器模拟 IDENTITY/AUTOINCRMENT 列有多糟糕?
我维护一个最初编写为特定于 SQL Server 的应用程序(使用 IDENTITY 字段)。因此,我们必须定义很多触发器来自动递增表的主键。
有人告诉我,这在 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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我的经验,这是一种非常常见的做法,而且并不是一种非常糟糕的做法。但是,如果您可以控制插入(例如,如果所有插入都是通过 PL/SQL API 完成的),那么直接在 INSERT 语句中使用序列会更有效 - 因为它避免了触发触发器的开销。但如果你使用了触发器,我真的不会过分担心!
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!
这可能不完全相关,但使用“插入触发器之前”自动递增主键是一个不错的选择,尤其是在 Merge 语句中。如果直接在合并插入部分中使用序列,则即使进行更新也会调用该序列。
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.