Triggers

2,744 views

Published on

Published in: Business, Economy & Finance
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
2,744
On SlideShare
0
From Embeds
0
Number of Embeds
194
Actions
Shares
0
Downloads
139
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Triggers

  1. 1. Trigger<br /><ul><li>Trigger is a statement
  2. 2. This is executed automatically by the system as a side effect of a modification to the data base.
  3. 3. Example </li></ul>Atm card, on-line reservation.<br />
  4. 4. Compopnents Trigger <br />Trigger statement:<br />The DML statement(insert,update,delete) is called the trigger statement. It fires the trigger body.<br />Trigger body:<br />The pl/sql block that is executed when a triggering statement is issued is called the trigger body.<br />Trigger Restriction:<br />This is on optional part. The restriction is imposed using when clause and is applicable only for row triggers.<br />
  5. 5. Types of trigger<br /><ul><li>Before
  6. 6. After
  7. 7. For each row
  8. 8. For each statement(default)</li></li></ul><li>Before/after<br /><ul><li>The BEFORE (or AFTER) in the trigger definition refers to when you want to run the trigger, either before the actual database modification (update, delete, insert) or after the actual database modification (update, delete, insert).</li></li></ul><li>Row level trigger<br />for example, when you’re doing an update, you can have a trigger fire once for each thing being updated (if you update 20 rows, the thing would fire 20 times), or you can have it fire just once per statement (if a single update statement is updating 20 rows, the trigger would fire just once) .This is what that for each row in the trigger definition means.<br />
  9. 9. Statement level trigger<br /><ul><li>Statement level triggers are triggered only once.</li></li></ul><li>Trigger event<br /><ul><li>Triggers events are events that initiate the trigger action.
  10. 10. Insert
  11. 11. Update
  12. 12. delete</li></li></ul><li>Built in trigger<br />CREATE [OR REPLACE]TRIGGER trigger name{BEFORE/AFTER} {SERVER ERROR/LOGON/LOGOFF/SHUTDOWN/STARTUP} ON DATABASE.<br /> begin<br /> pl/sql statements<br />end;<br />
  13. 13. <ul><li>Server error – trigger will be fired when server error message happens.
  14. 14. Log on  trigger will be fired after the client application logs on to the database successfully.
  15. 15. Log off  trigger will be fired before the client application </li></li></ul><li>Select * from branch<br />
  16. 16. Creating trigger(before)<br /><ul><li>set serveroutput on
  17. 17. create or replace trigger rowins1 before insert on branch
  18. 18. declare
  19. 19. x number;
  20. 20. begin
  21. 21. select count(*) into x from branch;
  22. 22. dbms_output.put_line(' number of available records before insert :' ||x);
  23. 23. end;</li></li></ul><li>output<br /><ul><li>insert into branch values('east',1700000,'delhi');
  24. 24. output:
  25. 25. number of available records before insert :2 1 row created.</li></li></ul><li>select * from branch;<br />insert into branch values(&apos;west&apos;,2300000,&apos;banga&apos;);<br />number of available records before insert :3 1 row created.<br />
  26. 26. Creating trigger (after)<br /><ul><li>set serveroutput on
  27. 27. create or replace trigger rowins2 after insert on branch
  28. 28. declare
  29. 29. x number;
  30. 30. begin
  31. 31. select count(*) into x from branch;
  32. 32. dbms_output.put_line(' number of available records after insert :' ||x);
  33. 33. end;</li></li></ul><li>output<br /><ul><li>insert into branch values('east',120000,'madurai');
  34. 34. number of available records before insert :4 number of available records after insert :5 1 row created.
  35. 35. alter trigger rowins1 disable;
  36. 36. Trigger altered.
  37. 37. insert into branch values('east',1450000,'nellai')
  38. 38. number of available records after insert :6
  39. 39. 1 row created.</li></li></ul><li>desc dba_triggers;<br />
  40. 40. select trigger_name,trigger_body from user_triggers;<br />
  41. 41. Parts of trigger<br />Trigger statement<br />Trigger body<br />Trigger restriction<br />
  42. 42. Data base trigger<br />A database trigger is a stored procedure.<br />It is fired implicitly when an insert,update or delete statements are issued against the table on which the trigger is defined<br />
  43. 43. Purpose of trigger<br />To enforce complex integrity constraints.<br />To enforce complex business rules.<br />To maintain a replicate table.<br />To generate data automatically<br />To avoid invalid transaction<br />
  44. 44. Department of CE/ITMSPVL Polytechnic College Pavoorchatram<br />

×