December 22, 2012

Triggers in SQL

Triggers


Triggers is a special kind of parsed , pre- compiled stored procedure that is invoked whenever DML command i.e data manipulation occurs like INSERT , UPDATE, DELETE.

Triggers used to help, ensure the relational integrity of database.

Triggers can not called / invoked directly by an application , where as stored procedure can be called directly by an application.

Triggers are automatically invoked by SQL SERVER.

Instead, an SQL trigger is invoked by the database management system on the execution of a triggering insert, update, or delete operation. The definition of the SQL trigger is stored in the database management system and is invoked by the database management system, when the SQL table, that the trigger is defined on, is modified.
Syntax of Creating a triggers

Triggers are created with the CREATE TRIGGER statement. This statement specifies that the on which table trigger is defined and on which events trigger will be invoked.

To drop Trigger one can use DROP TRIGGER statement.

CREATE TRIGGER EmployeeNameUpdate

ON dbo.Employees

For INSERT as  

/* Write here your logic */

Update table Employeesset FullName = FirstName + '' + lastName

GO


Ponits to be Remember :
  • We can create only three type of trigger on a table : UPDATE ,INSERT,DELETE
  • Rights to create  a trigger is only with the owner of the table.
  • We can not create a triggers with view or temporary table , but we can use them in creation of trigger on a another table.
  • On dropping a table all triggers associated to the triggers are automatically dropped .

 
 
 

No comments:

Post a Comment