Monday, January 25, 2016

Database Triggers?


In this post I am going to mention details of Database Triggers on a high level.

Database Triggers: 
Is a block of statements that are implicitly fired by a database when INSERT, UPDATE, DELETE statements issued against given table.
  • Triggers are stored in a database separately from their associated tables. 
  • Triggers can be defined only on tables, not on views except instead-of Trigger. 
  • However, triggers on base table(s) of a view are fired if an INSERT, UPDATE, or DELETE statement is issued against a view. 
Trigger has three main parts:
  1. Trigger Event or Statement: This can be an Insert, Update, Delete.
  2. Trigger Constraint or  Restriction: Optional. (a Boolean expression must be true for a trigger to fire.)
  3. Trigger Action: It is a procedure that contains a SQL statement along with other criteria.
Trigger configuration: We can have Triggers at the following levels...
  1. STATEMENT level
  2. ROW level
  3. On INSERT or UPDATE or DELETE operations levels
  4. Also, can be executed at BEFORE or AFTER events
Trigger Management:
  • We can Drop Trigger 
  • DISABLE or ENABLE Triggers from firing at any specific event or statement (see above)
Special Trigger category:
  • INSTEAD OF Triggers: 
    • It provide a way of modifying views that can not be modified directly using DML (Insert. Update. & Delete) statements.
    • It is called INSTEAD OF trigger because, unlike other types of triggers, a database fires the  trigger instead of executing the triggering statement
    • We can write normal INSERT, UPDATE, and DELETE statements against the view and INSTEAD OF trigger is fired to update the underlying tables appropriately. 
    • INSTEAD OF triggers are activated for each row of the view that gets modified. 
References:

0 comments:

Post a Comment