A trigger is an event within the DBMS that can cause some code to execute automatically. : Introduction « Trigger « Oracle PL/SQL Tutorial






There are four types of database triggers:

  1. Table-level triggers can initiate activity before or after an INSERT, UPDATE, or DELETE event.
  2. View-level triggers defines what can be done to the view.
  3. Database-level triggers can be activated at startup and shutdown of a database.
  4. Session-level triggers can be used to store specific information.
SQL>
SQL>
SQL> create table company(
  2     product_id        number(4)    not null,
  3     company_id          NUMBER(8)    not null,
  4     company_short_name  varchar2(30) not null,
  5     company_long_name   varchar2(60)
  6  );

Table created.

SQL> insert into company values(1,1001,'A Inc.','Long Name A Inc.');

1 row created.

SQL> insert into company values(1,1002,'B Inc.','Long Name B Inc.');

1 row created.

SQL> insert into company values(1,1003,'C Inc.','Long Name C Inc.');

1 row created.

SQL> insert into company values(2,1004,'D Inc.','Long Name D Inc.');

1 row created.

SQL> insert into company values(2,1005,'E Inc.','Long Name E Inc.');

1 row created.

SQL> insert into company values(2,1006,'F Inc.','Long Name F Inc.');

1 row created.

SQL>
SQL> create table product_audit(
  2     product_id number(4) not null,
  3     num_rows number(8) not null
  4  );

Table created.

SQL>
SQL>
SQL> CREATE OR REPLACE TRIGGER myTrigger
  2  AFTER INSERT ON company
  3  FOR EACH ROW
  4  BEGIN
  5    UPDATE product_audit
  6    SET num_rows =num_rows+1
  7    WHERE product_id =:NEW.product_id;
  8    IF (SQL%NOTFOUND) THEN
  9      INSERT INTO product_audit VALUES (:NEW.product_id,1);
 10    END IF;
 11  END;
 12  /

Trigger created.

SQL>
SQL> drop table product_audit;

Table dropped.

SQL>
SQL> drop table company;

Table dropped.

SQL>
SQL>








28.1.Introduction
28.1.1.A trigger is an event within the DBMS that can cause some code to execute automatically.
28.1.2.Placing triggers on tables
28.1.3.Trigger Which Modifies a Mutating Table
28.1.4.Solution for the Mutating Tables Problem
28.1.5.Raise Exception from trigger
28.1.6.Trigger for auditing
28.1.7.Trigger with a REFERENCING clause
28.1.8.Trigger with REFERENCING and WHEN clauses
28.1.9.Trigger with multiple triggering events