KEMBAR78
Lecture Notes Unit5 chapter16 Trigger Creation | PDF
RDBMS - Unit V
Chapter 16
TRIGGER
Prepared By
Dr. S.Murugan, Associate Professor
Department of Computer Science,
Alagappa Government Arts College, Karaikudi.
(Affiliated by Alagappa University)
Mailid: muruganjit@gmail.com
Reference Book:
LEARN ORACLE 8i, JOSE A RAMALHO
TRIGGER - Definition
➢ Triggers are routines or procedures that are used when
an INSERT, UPDATE, or DELETE command is
executed in a table or a view..
Difference between Trigger and Procedure
➢ A trigger is automatically executed without any action
required by the user.
➢ A stored procedure, on the other hand, needs to be
explicitly invoked.
TRIGGER – Merits and Demerits
Advantages of Trigger
➢ The major application of a trigger is to create
consistencies to the database.
➢ Create validation mechanisms.
Restrictions
➢ A trigger cannot execute the COMMIT, ROLLBACK,
or SAVEPOINT commands.
➢ It also cannot call procedures or functions
TRIGGER – Structure
Components of a Trigger
A trigger has three parts:
1. SQL command that activates the trigger (triggering
event)
The trigger can be activated by a SQL command or by a
user event
2. Trigger restriction
Represented by the WHEN clause, it specifies what
condition must be true for the trigger to be triggered.
3. Trigger action
The PL/SQL block, or Java or C routine executed by the
trigger.
Types of Triggers
Oracle8i has four types of triggers, depending on the
application:
➢ Table triggers applied to DML commands.
➢ INSTEAD OF triggers applied to views.
➢ SYSTEM EVENT triggers applied to the database in
the startup, shutdown, and error handling operations.
➢ USER EVENT triggers applied to a user or object
schema.
Types of Triggers - Table Triggers
➢ There are two distinct types of triggers that can be
used in a table.
➢ The first type, statement-level and the other type, row-
level.
➢ Statement-level triggers are triggered only once. For
example, when an UPDATE command updates 15
rows, the commands contained in the trigger are
executed only once, and not with every processed row.
➢ row-level triggers are executed on all the rows that
are affected by the command that enabled the trigger.
Types of Triggers – Instead of Triggers
➢ The INSTEAD OF trigger is available only in the
Enterprise version of Oracle.
Types of Triggers – System Event and User Event Triggers
➢ A trigger is created with the CREATE TRIGGER
command. It has the following syntax:
Modifying a Trigger - Enabling/Disabling a Trigger
➢ When a trigger is created, it is automatically enabled
and is triggered whenever the triggering command and
the execution command is true.
➢ To disable the execution of the trigger, use the ALTER
TRIGGER command with the DISABLE clause. To
enable it again, use the ENABLE clause:
Deleting a Trigger
➢ To delete a trigger, use the DROP TRIGGER
command:
Syntax:
DROP TRIGGER name_of_trigger;
➢ This removes the trigger structure from the database
References to Columns Inside a Trigger
➢ You can access the value of a column inside a row-
level trigger.
➢ For an INSERT command, the values of the fields that
will be written must be preceded by :new.
➢ For a DELETE command, the values of the row fields
that are being processed must be preceded by :old.
➢ For an UPDATE command, the original value that is
being written is accessed with :old. The new values
that will be written must be preceded by :new.
Validating the Data Entry
➢ create table emp1(name varchar(20), salary
number(7), eno number(5));
➢ CREATE OR REPLACE TRIGGER TESTASAL
BEFORE INSERT OR UPDATE OF SALARY ON
SYSTEM.EMP1 FOR EACH ROW
BEGIN
IF :NEW.SALARY > 8000 THEN
RAISE_APPLICATION_ERROR(-
20000,'INCORRECT VALUE');
END IF;
END;
/
➢ Execute the program and try to insert values below
8000 and above 8000. see the result
Validating the Data Entry
➢ Here, if the salary is below 8000 accepted otherwise
the trigger displays the error messsage.
Replicating with Triggers
➢ Triggers are an efficient mechanism to replicate data
among tables.
➢ We can easily create triggers that insert, delete, or
modify another table based on the operations
performed in the current table.
Replicating with Triggers - insertion
1. create table emp3(name varchar(20), salary
number(7), eno number(5));
2. CREATE TRIGGER REP_INS
AFTER INSERT ON EMP1
FOR EACH ROW
BEGIN
INSERT INTO EMP3 VALUES (:NEW.name,
:NEW.salary, :NEW.eno);
END;
3. Execute the trigger
4. insert into emp1 values('ramesh',5000,105) ;
(Whatever the changes made in emp1, it will reflect to
emp2.)
5. Select * from emp3;
Replicating with Triggers - Deletion
1. Create the trigger
CREATE TRIGGER REP_DEL
BEFORE DELETE ON EMP1
FOR EACH ROW
BEGIN
DELETE FROM EMP3
WHERE eno= :OLD.eno;
END;
/
2. Delete the record from emp1.
Delete from emp1 where eno=105.
3. Check the emp3 table, the eno=105 also deleted.
Select * from emp3;
Obtaining Information about a Trigger
➢ To view the DBA_TRIGGERS. (using
System/manager )
SQL> desc dba_triggers;
➢ To view the user’s triggers, you use the view
USER_TRIGGERS:
SQL> describe user_triggers;
➢ To view the trigger name and body of the trigger
SQL> select trigger_name,trigger_body from
user_triggers where trigger_name=’REP_INS’;

Lecture Notes Unit5 chapter16 Trigger Creation

  • 1.
    RDBMS - UnitV Chapter 16 TRIGGER Prepared By Dr. S.Murugan, Associate Professor Department of Computer Science, Alagappa Government Arts College, Karaikudi. (Affiliated by Alagappa University) Mailid: muruganjit@gmail.com Reference Book: LEARN ORACLE 8i, JOSE A RAMALHO
  • 2.
    TRIGGER - Definition ➢Triggers are routines or procedures that are used when an INSERT, UPDATE, or DELETE command is executed in a table or a view.. Difference between Trigger and Procedure ➢ A trigger is automatically executed without any action required by the user. ➢ A stored procedure, on the other hand, needs to be explicitly invoked.
  • 3.
    TRIGGER – Meritsand Demerits Advantages of Trigger ➢ The major application of a trigger is to create consistencies to the database. ➢ Create validation mechanisms. Restrictions ➢ A trigger cannot execute the COMMIT, ROLLBACK, or SAVEPOINT commands. ➢ It also cannot call procedures or functions
  • 4.
  • 5.
    Components of aTrigger A trigger has three parts: 1. SQL command that activates the trigger (triggering event) The trigger can be activated by a SQL command or by a user event 2. Trigger restriction Represented by the WHEN clause, it specifies what condition must be true for the trigger to be triggered. 3. Trigger action The PL/SQL block, or Java or C routine executed by the trigger.
  • 6.
    Types of Triggers Oracle8ihas four types of triggers, depending on the application: ➢ Table triggers applied to DML commands. ➢ INSTEAD OF triggers applied to views. ➢ SYSTEM EVENT triggers applied to the database in the startup, shutdown, and error handling operations. ➢ USER EVENT triggers applied to a user or object schema.
  • 7.
    Types of Triggers- Table Triggers ➢ There are two distinct types of triggers that can be used in a table. ➢ The first type, statement-level and the other type, row- level. ➢ Statement-level triggers are triggered only once. For example, when an UPDATE command updates 15 rows, the commands contained in the trigger are executed only once, and not with every processed row. ➢ row-level triggers are executed on all the rows that are affected by the command that enabled the trigger.
  • 8.
    Types of Triggers– Instead of Triggers ➢ The INSTEAD OF trigger is available only in the Enterprise version of Oracle.
  • 9.
    Types of Triggers– System Event and User Event Triggers ➢ A trigger is created with the CREATE TRIGGER command. It has the following syntax:
  • 10.
    Modifying a Trigger- Enabling/Disabling a Trigger ➢ When a trigger is created, it is automatically enabled and is triggered whenever the triggering command and the execution command is true. ➢ To disable the execution of the trigger, use the ALTER TRIGGER command with the DISABLE clause. To enable it again, use the ENABLE clause:
  • 11.
    Deleting a Trigger ➢To delete a trigger, use the DROP TRIGGER command: Syntax: DROP TRIGGER name_of_trigger; ➢ This removes the trigger structure from the database
  • 12.
    References to ColumnsInside a Trigger ➢ You can access the value of a column inside a row- level trigger. ➢ For an INSERT command, the values of the fields that will be written must be preceded by :new. ➢ For a DELETE command, the values of the row fields that are being processed must be preceded by :old. ➢ For an UPDATE command, the original value that is being written is accessed with :old. The new values that will be written must be preceded by :new.
  • 13.
    Validating the DataEntry ➢ create table emp1(name varchar(20), salary number(7), eno number(5)); ➢ CREATE OR REPLACE TRIGGER TESTASAL BEFORE INSERT OR UPDATE OF SALARY ON SYSTEM.EMP1 FOR EACH ROW BEGIN IF :NEW.SALARY > 8000 THEN RAISE_APPLICATION_ERROR(- 20000,'INCORRECT VALUE'); END IF; END; / ➢ Execute the program and try to insert values below 8000 and above 8000. see the result
  • 14.
    Validating the DataEntry ➢ Here, if the salary is below 8000 accepted otherwise the trigger displays the error messsage.
  • 15.
    Replicating with Triggers ➢Triggers are an efficient mechanism to replicate data among tables. ➢ We can easily create triggers that insert, delete, or modify another table based on the operations performed in the current table.
  • 16.
    Replicating with Triggers- insertion 1. create table emp3(name varchar(20), salary number(7), eno number(5)); 2. CREATE TRIGGER REP_INS AFTER INSERT ON EMP1 FOR EACH ROW BEGIN INSERT INTO EMP3 VALUES (:NEW.name, :NEW.salary, :NEW.eno); END; 3. Execute the trigger 4. insert into emp1 values('ramesh',5000,105) ; (Whatever the changes made in emp1, it will reflect to emp2.) 5. Select * from emp3;
  • 17.
    Replicating with Triggers- Deletion 1. Create the trigger CREATE TRIGGER REP_DEL BEFORE DELETE ON EMP1 FOR EACH ROW BEGIN DELETE FROM EMP3 WHERE eno= :OLD.eno; END; / 2. Delete the record from emp1. Delete from emp1 where eno=105. 3. Check the emp3 table, the eno=105 also deleted. Select * from emp3;
  • 18.
    Obtaining Information abouta Trigger ➢ To view the DBA_TRIGGERS. (using System/manager ) SQL> desc dba_triggers; ➢ To view the user’s triggers, you use the view USER_TRIGGERS: SQL> describe user_triggers; ➢ To view the trigger name and body of the trigger SQL> select trigger_name,trigger_body from user_triggers where trigger_name=’REP_INS’;