Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Oracle Database Trigger

2,316 views

Published on

Presentation about Trigger in Oracle database.
Advanced Database Lab - Informatics Engineering, Bakrie University

Published in: Technology
  • Be the first to comment

Oracle Database Trigger

  1. 1. Oracle DatabaseTriggerEryk Budi Pratama – Dimas Aryo AnggoroAdvanced Database LabInformatics Engineering –Bakrie University
  2. 2. Definition• Trigger is a series of PL/SQL statements attachedto a database table that execute whenever atriggering event (select, update, insert, delete)occurs.• Unlike stored procedures and functions, theynot explicitly called, but they are activated whena triggering event occurs.• Main purpose is to implement the complexintegrity constraints that can’t be done with theCREATE TABLE or ALTER TABLE command.
  3. 3. Trigger Types• Application TriggerTrigger will be activated if there is an event oncertain application• Database TriggerTrigger will be activated if there is a data event(DML Operation – Insert, Update, Delete) orsystem event (logon or shutdown) on a schemaor database
  4. 4. Trigger Timing• BEFOREwhere a trigger will be activated before DML process ontable occur• AFTERwhere a trigger will be activated after DML process ontable occur• INSTEAD OFTrigger that just functionate on VIEW and usually used toupdate data on complex view
  5. 5. SyntaxCREATE [OR REPLACE]TRIGGER trigger_nameBEFORE (or AFTER)INSERT OR UPDATE [OF COLUMNS] OR DELETEON tablename[FOR EACH ROW [WHEN (condition)]]BEGIN...END;
  6. 6. Example
  7. 7. Sample TableCREATE TABLE PERSON (ID INT,NAME VARCHAR(30),DOB DATE,PRIMARY KEY(ID));
  8. 8. Before Insert TriggerCREATE OR REPLACETRIGGER PERSON_INSERT_BEFOREBEFOREINSERTON PERSONFOR EACH ROWBEGINDBMS_OUTPUT.PUT_LINE(’BEFORE INSERT OF ’ ||:NEW.NAME);END;
  9. 9. (cont.)• The single INSERT statement fires the trigger. When werun it, we get the print out of ’BEFORE INSERT OF JOHNDOE’.SQL> INSERT INTO PERSON(ID,NAME,DOB) VALUES (1, ’ANI’,SYSDATE);BEFORE INSERT OF ANI1 row created.
  10. 10. After Insert TriggerCREATE OR REPLACETRIGGER PERSON_INSERT_AFTERAFTERINSERTON PERSONFOR EACH ROWBEGINDBMS_OUTPUT.PUT_LINE(’AFTER INSERT OF ’ || :NEW.NAME);END;
  11. 11. (cont.)SQL> INSERT INTO PERSON(ID,NAME,DOB) VALUES (2, ’BUDI’,SYSDATE);BEFORE INSERT OF BUDIAFTER INSERT OF BUDI1 row created.
  12. 12. Before Update TriggerCREATE OR REPLACETRIGGER PERSON_UPDATE_S_BEFOREBEFORE UPDATEON PERSONBEGINDBMS_OUTPUT.PUT_LINE(’BEFORE UPDATING SOMEPERSON(S)’);END;
  13. 13. (cont.)SQL> UPDATE PERSON SET DOB = SYSDATE;BEFORE UPDATING SOME PERSON(S)2 rows updated
  14. 14. Managing Trigger• Enable TriggerALTER TRIGGER trigger_name ENABLE• Disable TriggerALTER TRIGGER trigger_name DISABLE• Enable or Disable All TriggerALTER TABLE table_name DISABLE | ENABLE ALL TRIGGERS• Delete TRIGGERDROP TRIGGER nama_trigger
  15. 15. PermissionsJust like with procedures and functions, creating triggersrequires certain privileges which are not part of the defaultprivilege set.GRANT CREATE TRIGGER TO <username>;
  16. 16. Q&A

×