Triggers
Upcoming SlideShare
Loading in...5
×
 

Triggers

on

  • 3,114 views

 

Statistics

Views

Total Views
3,114
Views on SlideShare
2,978
Embed Views
136

Actions

Likes
1
Downloads
119
Comments
1

3 Embeds 136

http://www.ustudy.in 97
http://www.slideshare.net 30
http://ustudy.in 9

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Nice Work.....................]
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Triggers Triggers Presentation Transcript

  • Trigger
    • Trigger is a statement
    • This is executed automatically by the system as a side effect of a modification to the data base.
    • Example
    Atm card, on-line reservation.
  • Compopnents Trigger
    Trigger statement:
    The DML statement(insert,update,delete) is called the trigger statement. It fires the trigger body.
    Trigger body:
    The pl/sql block that is executed when a triggering statement is issued is called the trigger body.
    Trigger Restriction:
    This is on optional part. The restriction is imposed using when clause and is applicable only for row triggers.
  • Types of trigger
    • Before
    • After
    • For each row
    • For each statement(default)
  • Before/after
    • 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).
  • Row level trigger
    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.
  • Statement level trigger
    • Statement level triggers are triggered only once.
  • Trigger event
    • Triggers events are events that initiate the trigger action.
    • Insert
    • Update
    • delete
  • Built in trigger
    CREATE [OR REPLACE]TRIGGER trigger name{BEFORE/AFTER} {SERVER ERROR/LOGON/LOGOFF/SHUTDOWN/STARTUP} ON DATABASE.
    begin
    pl/sql statements
    end;
    • Server error – trigger will be fired when server error message happens.
    • Log on  trigger will be fired after the client application logs on to the database successfully.
    • Log off  trigger will be fired before the client application
  • Select * from branch
  • Creating trigger(before)
    • set serveroutput on
    • create or replace trigger rowins1 before insert on branch
    • declare
    • x number;
    • begin
    • select count(*) into x from branch;
    • dbms_output.put_line(' number of available records before insert :' ||x);
    • end;
  • output
    • insert into branch values('east',1700000,'delhi');
    • output:
    • number of available records before insert :2 1 row created.
  • select * from branch;
    insert into branch values('west',2300000,'banga');
    number of available records before insert :3 1 row created.
  • Creating trigger (after)
    • set serveroutput on
    • create or replace trigger rowins2 after insert on branch
    • declare
    • x number;
    • begin
    • select count(*) into x from branch;
    • dbms_output.put_line(' number of available records after insert :' ||x);
    • end;
  • output
    • insert into branch values('east',120000,'madurai');
    • number of available records before insert :4 number of available records after insert :5 1 row created.
    • alter trigger rowins1 disable;
    • Trigger altered.
    • insert into branch values('east',1450000,'nellai')
    • number of available records after insert :6
    • 1 row created.
  • desc dba_triggers;
  • select trigger_name,trigger_body from user_triggers;
  • Parts of trigger
    Trigger statement
    Trigger body
    Trigger restriction
  • Data base trigger
    A database trigger is a stored procedure.
    It is fired implicitly when an insert,update or delete statements are issued against the table on which the trigger is defined
  • Purpose of trigger
    To enforce complex integrity constraints.
    To enforce complex business rules.
    To maintain a replicate table.
    To generate data automatically
    To avoid invalid transaction
  • Department of CE/ITMSPVL Polytechnic College Pavoorchatram