Professional Documents
Culture Documents
Summary: in this tutorial, you will learn how to use an Oracle INSTEAD OF trigger to insert data
into tables via a non-updatable view.
If the view has an INSTEAD OF trigger, it will automatically skip the DML statement and
execute other DML statements instead.
Note that an INSTEAD OF trigger is fired for each row of the view that gets modified.
In Oracle, you can create an INSTEAD OF trigger for a view only. You cannot create an
INSTEAD OF trigger for a table.
In this syntax:
First, specify the name of the trigger after the CREATE TRIGGER keywords. Use OR
REPLACE if you want to modify an existing trigger.
Third, specify the name of the view with which the trigger is associated.
Finally, specify the code that executes instead of the INSERT , UPDATE , and DELETE .
We will use the customers and contacts tables from the sample database
(https://www.oracletutorial.com/getting-started/oracle-sample-database/) for the demonstration.
INSERT INTO
vw_customers(
name,
address,
website,
credit_limit,
first_name,
last_name,
email,
phone
)
VALUES(
'Lam Research',
'Fremont, California, USA',
'https://www.lamresearch.com/',
2000,
'John',
'Smith',
'john.smith@lamresearch.com',
'+1-510-572-0200'
);
SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserve
01779. 00000 - "cannot modify a column which maps to a non key-preserved tab
*Cause: An attempt was made to insert or update columns of a join view whi
map to a non-key-preserved table.
*Action: Modify the underlying base tables directly.
INSERT INTO
vw_customers(
name,
address,
website,
credit_limit,
first_name,
last_name,
email,
phone
)
VALUES(
'Lam Research',
'Fremont, California, USA',
'https://www.lamresearch.com/',
2000,
'John',
'Smith',
'john.smith@lamresearch.com',
'+1-510-572-0200'
);
As you can see, the new customer has been inserted successfully.
In this tutorial, you have learned how to use an Oracle INSTEAD OF trigger to insert data into
the base tables via a non-updatable view.
Copyright © 2020 Oracle Tutorial. All Rights Reserved.