Trigger
Trigger
• Trigger:
• Row level triggers are created using the FOR EACH ROW clause in the
CREATE TRIGGER command.
• It fires for every record that got affected with the execution of DML
statements like INSERT, UPDATE, DELETE etc.
• Application:
• Consider a case where our requirement is to prevent
updation of empno 100 record cannot be updated, then whenever
UPDATE statement update records, there must be PL/SQL block that will
be fired automatically by UPDATE statement to check that it must not
be 100, so we have to use Row level Triggers for that type of applications.
Statement Level Triggers :
• Statement level triggers are triggered only once for each transaction.
•
Application:
Consider a case where our requirement is to prevent the DELETE
operation during Sunday. For this whenever DELETE statement deletes
records, there must be PL/SQL block that will be fired only once by DELETE
statement to check that day must not be Sunday by referencing system date,
so we have to use Statement level Trigger for which fires only once for above
application.
• 1. After Triggers:
• These triggers run after an insert, update or
delete on a table. They are not supported for
views.
• AFTER TRIGGERS can be classified further into
three types as:
• AFTER INSERT Trigger.
• AFTER UPDATE Trigger.
• AFTER DELETE Trigger.
• create trigger Trigger_name (before | after)
[insert | update | delete] on [table_name] [for
each row] [trigger_body]
• CREATE TRIGGER: These two keywords specify that a
triggered block is going to be declared.
• TRIGGER_NAME: It creates or replaces an existing trigger
with the Trigger_name. The trigger name should be unique.
• BEFORE | AFTER: It specifies when the trigger will be
initiated i.e. before the ongoing event or after the ongoing
event.
• INSERT | UPDATE | DELETE: These are the DML operations
and we can use either of them in a given trigger.
• ON[TABLE_NAME]: It specifies the name of the table on
which the trigger is going to be applied.
• FOR EACH ROW: Row-level trigger gets executed when any
row value of any column changes.
• TRIGGER BODY: It consists of queries that need to be
executed when the trigger is called.
• Benefits of Triggers
• Triggers can be written for the following purposes −
• Generating some derived column values automatically
• Enforcing referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions
• Select * from customers;
• +----+----------+-----+-----------+----------+
• | ID | NAME | AGE | ADDRESS | SALARY |
• +----+----------+-----+-----------+----------+
• | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
• | 2 | Khilan | 25 | Delhi | 1500.00 |
• | 3 | kaushik | 23 | Kota | 2000.00 |
• | 4 | Chaitali | 25 | Mumbai | 6500.00 |
• | 5 | Hardik | 27 | Bhopal | 8500.00 |
• | 6 | Komal | 22 | MP | 4500.00 |
• +----+----------+-----+-----------+----------+
• CREATE OR REPLACE TRIGGER display_salary_changes
• BEFORE DELETE OR INSERT OR UPDATE ON student12
• FOR EACH ROW
• WHEN (NEW.ID > 0)
• DECLARE
• sal_diff number;
• BEGIN
• sal_diff := :NEW.sal - :OLD.sal;
• dbms_output.put_line('Old salary: ' || :OLD.sal);
• dbms_output.put_line('New salary: ' || :NEW.sal);
• dbms_output.put_line('Salary difference: ' || sal_diff);
• END;
• /
• CREATE TABLE Student(
• studentID number,
• FName VARCHAR(20),
• LName VARCHAR(20),
• Address VARCHAR(30),
• City VARCHAR(15),
• Marks number,
• PRIMARY KEY(studentID)
• );
Trigger after update or delete
• SQL> create table q1 (name varchar(10),salary number(10));
• Table created.
•
• SQL> create table q2( backup)(name varchar(10),salary number(10));
• Table created.
•
• SQL> create trigger q3 after update or delete on q1 for each row
• 2 declare
• 3 oper varchar(10);
• 4 name varchar(10);
• 5 salary number(10);
• 6 begin
• 7 if updating then oper:='update';
• 8 end if;
• 9 if deleting then oper:='delete';
• 10 end if;
• 11 name:=:old.name;
• SQL> insert into q1 values('aman',3450);
• 1 row created.
•
• SQL> insert into q1 values('vasu',1230);
• 1 row created.
•
• SQL> select * from q1;
• NAME SALARY
• ---------- ----------
• aman 3450
• vasu 1230
•
• SQL> update q1 set salary=8000 where name='aman';
• 1 row updated.
•
• SQL> select * from q2;
• NAME SALARY
• ---------- ----------
• aman 3450
•
• SQL> select * from q1;
• NAME SALARY
• ---------- ----------
• aman 8000
• vasu 1230
Create trigger for before insertion or
update.
• SQL> create table students1(name varchar2(20),roll_no
varchar2(9) not null primary key, balance varchar2(10));
• Table created.
• select * from students1;