Oracle 视图不可更新,关于 Replace Of 触发器的建议
迁移系统/数据库后,我们修改了一个中央表,该表已用于与 15 个不同系统连接。我们使用此迁移来添加和删除该表中的一些字段。
为了保持与接口系统的直接兼容性(即只需要更改数据库链接),创建了一个视图,该视图显示与旧表完全相同的列。然而,其中一些列只是模拟的,因此该视图包含如下结构:
(...)
CREATE OR REPLACE VIEW STAFF_DATA_COMPAT AS
SELECT
NVL(knownas_surname,surname) as surname,
first_name
middle_name as mid-name
NULL as ni,
NULL as home_tel_no,
(...)
显然,该视图本质上不可更新。
我确实明白,所有 DML(插入、更新、删除)语句都需要 INSTEAD OF 触发器。 我可以看到,INSTEAD OF INSERT 触发器应该非常简单(只需在适当的情况下将 :NEW.field 插入到真实表中并忽略其他)。
但实际问题是:如何编写相应的 INSTEAD OF UPDATE/DELETE 触发器? 例如,如何接管原始 DELETE 语句的“WHERE”子句?使用这些触发器时还有什么我应该担心的副作用吗?
顺便提一句。这是Oracle 11g。
after migrating a system/database we modified a central table which has been used for interfacing with 15 different systems. We used this migration to add and delete a few fields in this table.
To maintain direct compatibility with the interfacing systems (i.e. only need to change the database-link), a view has been created which shows the exact same columns as the old table had. However, some of these columns are only emulated, so the view contains constructs like these:
(...)
CREATE OR REPLACE VIEW STAFF_DATA_COMPAT AS
SELECT
NVL(knownas_surname,surname) as surname,
first_name
middle_name as mid-name
NULL as ni,
NULL as home_tel_no,
(...)
Obviously, this view is not inherently updatable.
I do understand, that you need INSTEAD OF triggers for all DML (insert, update, delete) statements.
I can see, that a INSTEAD OF INSERT trigger should be quite straightforward (just inserting :NEW.field to the real table, where appropriate and ignoring the others).
But the actual question: How to write the according INSTEAD OF UPDATE/DELETE triggers? For instance, how do I take over the "WHERE" clause of an original DELETE statement? Is there anything else I should worry about, any side-effects when using these triggers?
Btw. It's Oracle 11g.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
INSTEAD OF 触发器如下所示(我假设您有一个主键列
id
):请注意,某些列实际上可能在原始视图中可更新。查询
all_updatable_columns
视图(在创建触发器之前)以查找:视图已创建
如果您只需要插入/更新这些列,则不需要 INSTEAD OF 触发器。
The INSTEAD OF trigger would look like this (I've assumed you have a primary key column
id
):Note that some columns may in fact be updatable in the original view. Query the
all_updatable_columns
view (before creating the trigger) to find out:View created
If you only need to insert/update these columns, you don't need an INSTEAD OF trigger.
INSTEAD OF 触发器隐式地是“FOR EACH ROW”,因此您不必找出 WHERE 子句,只需执行以下操作:
这也显示了 INSTEAD OF 触发器的缺点之一:它们逐行工作不是成套的。
INSTEAD OF triggers are implicitly "FOR EACH ROW", so you don't have to find out the WHERE clause, you just do something like this:
This also shows one of the drawbacks of INSTEAD OF triggers: they work row-by-row not in sets.