Presentation is loading. Please wait.

Presentation is loading. Please wait.

Triggers.

Similar presentations


Presentation on theme: "Triggers."— Presentation transcript:

1 Triggers

2 Triggers A database trigger is a stored procedure that is fired when an insert, update or delete statement is issued against the associated table.

3 Database triggers can be used for the following purposes.
To generate data automatically. To enforce complex integrity constraints. (e.g. checking with sysdate, checking with data in another table). To customize complex security authorizations. To maintain replicate tables. To audit data modifications.

4 Syntax Create or replace trigger <trigger_name>
[before / after] [insert/update/delete] on <tablename> [for each statement / for each row] [when <condition>];

5 Parts of trigger Trigger statement Trigger body Trigger restriction

6 Trigger statement Trigger body Trigger Restriction
The trigger statement specifies the DML statements like update, delete and insert and it fires the trigger body. It also specifies the table to which the trigger is associated. Trigger body Trigger body is a PL/SQL block that is executed when a triggering statement is issued. Trigger Restriction Restrictions on a trigger can be achieved using the WHEN clause as shown in the syntax for creating triggers. They can be included in the definition of a row trigger, wherein, the condition in the

7 Types of triggers Before After For each row
For each statement (default)

8 Example SQL> ed create or replace trigger t_emp before insert or update on emp for each row declare cursor st is select eno from salesman; begin if :new.ename is null then raise_application_error(-20001,'NULL NOT ALLOWED'); end if; end; SQL> / Trigger created. SQL> insert into emp values(''); insert into emp values('') * ERROR at line 1: ORA-20001: NULL NOT ALLOWED ORA-06512: at "ME05.DATVALID", line 5 ORA-04088: error during execution of trigger 'ME12.DATVALID'

9 Example -2 SQL> create or replace trigger trigger4 after delete on dept for each row begin delete from emp where dno=:old.dno; end; SQL> delete from dept where dno=10; SQL> Select * from dept; SQL> Select * from emp;

10 SQL> create table dml_tab (action varchar(15), atime date);
SQL> alter SESSION NET NLS_DATE_FORMAR=‘DD-MON-YYYY HH24:MI:SS’; SQL> create or replace trigger tigger5 After insert or delete or update on emp Begin If inserting then Insert into dml_tab values (‘INSERT’, sysdate); End if; If deleting then Insert into dml_tab values (‘DELETE’, sysdate); If updating (salary) then Insert into dml_tab values (‘UPDATE’, sysdate); End; SQL > insert into emp values ( 101,’Raja’, 20000,10,300); SQL> delete emp where eno=100; SQL> update emp set salary=25000 where eno=200; SQL> select * from dml_tab;

11 Client/Server Systems
Networked computing model Processes distributed between clients and servers Client–Workstation (usually a PC) that requests and uses a service Server–Computer (PC/mini/mainframe) that provides a service For DBMS, server is a database server

12 database server architecture


Download ppt "Triggers."

Similar presentations


Ads by Google