The Oracle INSTEAD-OF trigger has the
ability to update normally non-updateable views. Simple views are
generally updateable via DML statements issued against the view.
However, when a view becomes more complex it may lose its
“updateable-ness,” and the Oracle INSTEAD-OF trigger must be used.
If a view is inherently updatable and has
Restrictions on INSTEAD OF Triggers
INSTEAD OF triggers are valid for DML events on views. They are not valid for DDL or database events.If a view is inherently updatable and has
INSTEAD OF
triggers, then the triggers take preference. In other words, the
database fires the triggers instead of performing DML on the view.Restrictions on INSTEAD OF Triggers
- INSTEAD OF triggers are valid only for views. You cannot specify an INSTEAD OF trigger on a table.
- You can read both the :OLD and the :NEW value, but you cannot write either the :OLD or the :NEW value.
- In this example, an
order_infoview is created to display information about customers and their orders:
Normally this view would not be updatable, because the primary key of the1CREATEVIEWorder_infoAS2SELECTc.customer_id, c.cust_last_name, c.cust_first_name,3o.order_id, o.order_date, o.order_status4FROMcustomers c, orders o5WHEREc.customer_id = o.customer_id;orderstable (order_id) is not unique in the result set of the join view. To make this view updatable, create anINSTEADOFtrigger on the view to processINSERTstatements directed to the view.
You can now insert into both base tables through the view (as long as all01CREATEORREPLACETRIGGERorder_info_insert02INSTEADOFINSERTONorder_info03DECLARE04duplicate_info EXCEPTION;05PRAGMA EXCEPTION_INIT (duplicate_info, -00001);06BEGIN07INSERTINTOcustomers08(customer_id, cust_last_name, cust_first_name)09VALUES(10:new.customer_id,11:new.cust_last_name,12:new.cust_first_name);1314INSERTINTOorders (order_id, order_date, customer_id)15VALUES(16:new.order_id,17:new.order_date,18:new.customer_id);1920EXCEPTION21WHENduplicate_infoTHEN22RAISE_APPLICATION_ERROR (23num=> -20107,24msg=>'Duplicate customer or order ID');2526ENDorder_info_insert;NOTNULLcolumns receive values):
INSERT INTO order_info VALUES (999, 'Smith', 'John', 2500, '13-MAR-2001', 0);
No comments:
Post a Comment