SQL Tutorial

Introducing to Database Trigger

Triggers are a kind of stored procedure which are invoked automatically by database server when predefined events occurred. Events here can be altering the tables, drop the tables, adding or removing rows on a table. Trigger is a kind of stored procedure means it can contains declarative or procedural SQL statements except one thing it cannot be invoked explicitly by user or other stored procedures. It only can be invoked when a predefined event occurred. Because of this feature, triggers are usually consider as events in the database.

Triggers can be applied in some contexts :

  • Triggers can helps database administrator gather statistic data on table access.
  • Triggers can be used to provide transparent logging service to log data changes and then send notification to the database administrator.
  • Triggers can be used to generate and maintain derived data in derived column such as computed column.
  • Triggers can be used to enforce data integrity when the business rule of data is too complex and prevent invalid transaction.
  • And triggers can be used to enforce complex security authorization context..

Triggers are useful for use in the database but it is suggested that it should be used only when necessary. The triggers can put the burden of interdependency on the database and also burden of maintenance.

Triggers was added to SQL3 standard but different database servers implement trigger in different ways. So it is easier to demonstrate trigger in a specified database server. We will use Microsoft SQL Server 2005 for demonstration We surely also try to provide you resources where you can find how other database servers implements trigger.