Upload
vam1
View
216
Download
0
Embed Size (px)
Citation preview
7/28/2019 Oracle Trigger After Insert Example
1/4
Oracle Trigger After Insert Example & Create
The after insert trigger in oracle is a DML trigger which is fired by an insert statement on a table. The oracle after
insert trigger is used to update the values on the table and to log the inserts in another table. Both the insert and the
trigger executes in a single transaction. First the insert statement executes and then the trigger executes. As the insert
statement executes first, you cannot generate primary key values using the after insert trigger. User the before insert
trigger to generate the primary key values.
Oracle Trigger After Insert Example:
1. Updating values in the table
you can create after insert trigger to update the values in the table on which the insert statement is fired. See the
following example which updates the hire date of the employee
CREATE OR REPLACE TRIGGER update_employees
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
UPDATE employees
SET hire_date = sysdate
WHERE employee_id = :new.employee_id;
END update_employees;
/
By default an employees joining date is on the day which you insert a record into the employees table. So, you dont
need to explicitly specify the hire_date value in the insert statement. Just use the above trigger statement which
update the hire date when you insert a record into the employees table.
2. Logging the rows in another table.
You can use the after insert tigger to track the newly records created in a table in another log table. See the following
example:
CREATE OR REPLACE TRIGGER log_rows_products
AFTER INSERT ON products
FOR EACH ROWBEGIN
INSERT INTO products_changes
(
product_id,
product_name
)
VALUES
(
:new.product_id,
:new.product_name
);
END log_rows_products;
/
Whenever a new insert statement is fired on the products table, the above trigger gets fired and inserts the records
into another table (products_changes).
Oracle Trigger Before Insert Create & Example
The before insert trigger in oracle is a DML trigger which is fired by an insert statement on a table. The oracle
before insert trigger is used to generate primary key values on the table and to log the inserts in another table. Both
the insert and the trigger executes in a single transaction.
7/28/2019 Oracle Trigger After Insert Example
2/4
Oracle Trigger Before Insert Example:
1. Generating Primary key value for the table
you can create a trigger to generate a primary key value for the table on which the insert statement is fired. The
following example shows generating the primary key value for the employees table using the trigger:
CREATE OR REPLACE TRIGGER gen_pk_employees
BEFORE INSERT ON employees
FOR EACH ROW
BEGINSELECT emp_sequence.NEXTVAL INTO :new.employee_id
FROM DUAL;
END gen_pk_employees;
/
When you create the above trigger, you dont need to specify a value for the primary key column in the INSERT
statement. Note that the after insert trigger cannot generate a primary key value.
2. Logging the rows in another table.
You can use the tigger to log the newly inserted rows in a table in another table. The following trigger logs the new
rows in employees table:
CREATE OR REPLACE TRIGGER log_rows_employees
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employees_changes
(
employee_id,
first_name,
last_name
)
VALUES
(
:new.employee_id,
:new.first_name,
:new.last_name
);
END log_rows_employees;
/
Whenever a new insert statement is fired on the employees table, the above trigger gets fired and inserts the records
into another table (employees_changes).
Instead of Triggers on Views Example in Oracle
Instead of triggers in oracle database are defined on views only. A normal DML trigger executes only when a DMLoperation is issued on a table. Whereas instead of trigger fires when a DML statment is issued on the view. Instead-
of triggers must be row level.
Take a look at the following view defintion:
Create or replace view emp_dept_join as
Select d.department_id,
d.department_name,
e.first_name,
e.last_name
from employees e,
departments d
7/28/2019 Oracle Trigger After Insert Example
3/4
where e.department_id = d.department_id;
As the view consists of two table joins, it is illegal to insert records into this view as the insert requires both the
underlying tables to be modified.
By creating an instead-of trigger on the view, you can insert the records into both the underlying tables. An
examples is shown below:
CREATE OR REPLACE TRIGGER insert_emp_dept
INSTEAD OF INSERT ON emp_dept_join
DECLARE
v_department_id departments.department_id%TYPE;BEGIN
BEGIN
SELECT department_id INTO v_department_id
FROM departments
WHERE department_id = :new.department_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO departments (department_id, department_name)
VALUES (dept_sequence.nextval, :new.department_name)
RETURNING ID INTO v_department_id;
END;
INSERT INTO employees (employee_id, first_name, last_name, department_id)VALUES(emp_sequence.nextval, :new.first_name, :new.last_name, v_department_id);
END insert_emp_dept;
/
System Triggers Example in Oracle PLSQL
System Trigger fires when a system event like database startup, shutdown or DDL operations like creating objects
occur in oracle database. System triggers wont fire for DML operation.
We will create a system trigger which logs the object creation information in the current schema. First create the
following table in the oracle database:
CREATE TABLE Audit_DDL_OPS
(ObjectOwner varchar2(30),
ObjectType varchar2(30),
ObjectName varchar2(30),
CreationDate Date
);
Now create the following system trigger in the same schema:
CREATE OR REPLACE TRIGGER Audit_DDLS
AFTER CREATE ON SCHEMA
BEGIN
INSERT INTO Audit_DDL_OPS
(ObjectOwner,
ObjectType,
ObjectName,
CreationDate
)
VALUES
(ORA_DICT_OBJ_OWNER,
ORA_DICT_OBJ_TYPE,
ORA_OBJ_DICT_NAME,
SYSDATE
)
END;
/
7/28/2019 Oracle Trigger After Insert Example
4/4
Whenever a new object created in the database, the above system trigger logs the user information in the
Audit_DDL_OPS table.
DDL statements are not allowed in Procedures (PLSQL BLOCK)
PL/SQL objects are precompiled. All the dependencies are checked before the execution of the objects. This makes
the programs to execute faster.
The dependencies include database objects, Tables, Views, synonyms and other objects. The dependency does not
depend on the data.
As DML (Data Manipulation Language) statements do not change the dependency, they can run directly in PL/SQL
objects. On the other hand, DDL (Data Definition Language) statements like CREATE, DROP, ALTER commands
and DCL (Data Control Language) statements like GRANT, REVOKE can change the dependencies during the
execution of the program.
Example: Let say you have dropped a table during the execution of a program and later in the same program when
you try to insert a record in to that table the program will fail.
This is the reason why DDL statements are not allowed directly in PL/SQL programs.
Oracle Procedure To Disable All Triggers In A Schema(User)
The below procedure can be used to disable all the triggers in a schema in oracle database.
CREATE OR REPLACE PROCEDURE DISABLE_TRIGGERS
IS
v_statement VARCHAR2(500);
CURSOR trigger_cur
IS
SELECT trigger_name
FROM user_triggers;
BEGIN
FOR i in trigger_cur
LOOP
v_statement := 'ALTER TRIGGER '||y.trigger_name||' DISABLE';
EXECUTE IMMEDIATE v_statement;
END LOOP;
END;