By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,823 Members | 756 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

TRIGGERS - 1

debasisdas
Expert 5K+
P: 8,127
This thread contains some useful tips/sample codes regarding TRIGGERS in oracle, that the forum members may find useful.

TRIGGERS:
===============
Database trigger is a PL/SQL block that is executed on an event in the database.
The event is related to a particular data manipulation of a table such as inserting, deleting or updating a row of a table.

Triggers may be used :

1.To implement complex business rule, which cannot be implemented using integrity constraints.
2.To audit the process. For example, to keep track of changes made to a table.
3. To automatically perform an action when another concerned action takes place. For example, updating a table whenever there is an insertion of a row into another table.

Types of Triggers:
------------------------
Depending upon when a trigger is fired it may be classified as :

a.Statement-level trigger
b.Row-level trigger
c.Before triggers
d.After triggers
e.Instead of triggers

Syntax:
-----------
CREATE [OR REPLACE] TRIGGER triggername
{BEFORE | AFTER | INSTEAD OF}
{DELETE | INSERT | UPDATE [OF columns]}
[OR {DELETE | INSERT |UPDATE [OF columns]}]...
ON table_name|SCHEMA
[REFERENCING [OLD AS old] [NEW AS new]]
[FOR EACH ROW [WHEN condition]]
<PL/SQL block>

Trigger Sample Ex #1
====================

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE TRIGGER  MYTRIG AFTER INSERT OR DELETE ON EMP
  2. DECLARE
  3. MCOUNT NUMBER;
  4. BEGIN
  5. SELECT COUNT(*) INTO MCOUNT FROM EMP;
  6. --checks the number of records in the table.
  7. DBMS_OUTPUT.PUT_LINE('THE TOTAL NUMBER OF EMPLOYEE ARE:' || MCOUNT);
  8. --and displays the same after the event .
  9. END;
  10.  
  11.  
Trigger Sample Ex #2
================

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE TRIGGER ONLYPOSITIVE BEFORE INSERT OR UPDATE OF SAL ON EMP FOR EACH ROW
  2. BEGIN
  3. IF :NEW.SAL<0 THEN
  4. --checks of the value inserted into teh sal column is less than 0.
  5. RAISE_APPLICATION_ERROR(-20100,'PLEASE INSERT A POSITIVE VALUE');
  6. --if valus is less than 0 ,then raise the exception.
  7. END IF;
  8. END;
Note:-Trigger does not allow negative values to insert/update to the table.

trigger example #3
==================

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE TRIGGER NODELETE BEFORE INSERT OR UPDATE OR DELETE ON DEPT FOR EACH ROW
  2. BEGIN
  3. IF INSERTING THEN
  4. RAISE_APPLICATION_ERROR(-20020,'CAN''T INSERT NEW RECORDS TO THIS TABLE');
  5. ELSIF UPDATING THEN
  6. RAISE_APPLICATION_ERROR(-20021,'CAN''T UPDATE RECORDS OF THIS TABLE');
  7. ELSIF DELETING THEN
  8. RAISE_APPLICATION_ERROR(-20022,'YOU ARE NOT AUTHORISED TO DELETE THESE RECORDS');
  9. END IF;
  10. END;
Note:- the triggermakes the trigger readonly by not allowing any DML on it.

Trigger Sample Ex #4
================
Expand|Select|Wrap|Line Numbers
  1. create or replace trigger mytrig
  2. before insert on emp
  3. declare
  4. a char(3);
  5. begin
  6. --selects the system day to the vaiable.
  7. select to_char(sysdate,'dy') into a from dual;
  8. if a in('sun','sat') then
  9. --restrict any transaction on saturday and sunday.
  10. Raise_application_error(-20004,'Cannot do manipulation today');
  11. end if;
  12. end;
Note:--This trigger restricts transactions on the table on weekends.


Also check TRIGGERS - 2
Jun 13 '07 #1
Share this Article
Share on Google+