You are on page 1of 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 ROW BEGIN 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.

e. 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. d. Take a look at the following view defintion: Create or replace view emp_dept_join as Select d. You can use the tigger to log the newly inserted rows in a table in another table.first_name. Whereas instead of trigger fires when a DML statment is issued on the view. Logging the rows in another table. END gen_pk_employees. :new. END log_rows_employees.last_name ). last_name ) VALUES ( :new. e.department_name. Note that the after insert trigger cannot generate a primary key value.employee_id FROM DUAL. 2. 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 from employees e. A normal DML trigger executes only when a DML operation is issued on a table.employee_id. you dont need to specify a value for the primary key column in the INSERT statement. Insteadof triggers must be row level. departments d . / When you create the above trigger. 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 BEGIN SELECT emp_sequence. / Whenever a new insert statement is fired on the employees table. Instead of Triggers on Views Example in Oracle Instead of triggers in oracle database are defined on views only.Oracle Trigger Before Insert Example: 1. the above trigger gets fired and inserts the records into another table (employees_changes).department_id. first_name. :new.NEXTVAL INTO :new.

it is illegal to insert records into this view as the insert requires both the underlying tables to be modified. you can insert the records into both the underlying tables. ORA_OBJ_DICT_NAME. :new.department_id%TYPE.nextval. ObjectName varchar2(30).first_name. first_name. :new.department_id. v_department_id). last_name.department_name) RETURNING ID INTO v_department_id. INSERT INTO employees (employee_id. ObjectType. END insert_emp_dept. / . An examples is shown below: CREATE OR REPLACE TRIGGER insert_emp_dept INSTEAD OF INSERT ON emp_dept_join DECLARE v_department_id departments. SYSDATE ) END. ObjectType varchar2(30). shutdown or DDL operations like creating objects occur in oracle database. As the view consists of two table joins.nextval.last_name. 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. By creating an instead-of trigger on the view. System triggers wont fire for DML operation. department_id) VALUES(emp_sequence. / System Triggers Example in Oracle PLSQL System Trigger fires when a system event like database startup. ObjectName. BEGIN BEGIN SELECT department_id INTO v_department_id FROM departments WHERE department_id = :new. We will create a system trigger which logs the object creation information in the current schema. EXCEPTION WHEN NO_DATA_FOUND THEN INSERT INTO departments (department_id.department_id = d.department_id. First create the following table in the oracle database: CREATE TABLE Audit_DDL_OPS ( ObjectOwner varchar2(30).where e. CreationDate Date ). department_name) VALUES (dept_sequence. END. ORA_DICT_OBJ_TYPE. CreationDate ) VALUES (ORA_DICT_OBJ_OWNER. :new.

On the other hand. The dependencies include database objects. ALTER commands and DCL (Data Control Language) statements like GRANT. CREATE OR REPLACE PROCEDURE DISABLE_TRIGGERS IS v_statement VARCHAR2(500). 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.Whenever a new object created in the database. Tables. EXECUTE IMMEDIATE v_statement. DROP. DDL (Data Definition Language) statements like CREATE. REVOKE can change the dependencies during the execution of the program. . This is the reason why DDL statements are not allowed directly in PL/SQL programs. The dependency does not depend on the data. All the dependencies are checked before the execution of the objects. CURSOR trigger_cur IS SELECT trigger_name FROM user_triggers. END. BEGIN FOR i in trigger_cur LOOP v_statement := 'ALTER TRIGGER '||y. they can run directly in PL/SQL objects. This makes the programs to execute faster.trigger_name||' DISABLE'. As DML (Data Manipulation Language) statements do not change the dependency. DDL statements are not allowed in Procedures (PLSQL BLOCK) PL/SQL objects are precompiled. END LOOP. the above system trigger logs the user information in the Audit_DDL_OPS table. 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. Views. synonyms and other objects.