标签:
Link:
http://www.essentialsql.com/what-is-a-database-trigger/
Copy...
A database trigger is special stored procedure that is run when specific actions occur within a database. Most triggers are defined to run when changes are made to a table’s data. Triggers can be defined to run instead of or after DML (Data Manipulation Language) actions such as INSERT, UPDATE, and DELETE.
Triggers help the database designer ensure certain actions, such as maintaining an audit file, are completed regardless of which program or user makes changes to the data.
The programs are called triggers since an event, such as adding a record to a table, fires their execution.
Triggers and their implementations are specific to database vendors. In this article we’ll focus on Microsoft SQL server; however, the concepts are the same or similar in Oracle and MySQL.
Note: All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database. You can get started using these free tools using my Guide Getting Started Using SQL Server.
The triggers can occur AFTER or INSTEAD OF a DML action. Triggers are associated with the database DML actions INSERT, UPDATE, and DELETE. Triggers are defined to run when these actions are executed on a specific table.
Once the DML actions, such as an INSERT completes, the AFTER trigger executes. Here are some key characteristics of AFTER triggers:
INSTEAD OF triggers, as their name implies, run in place of the DML action which caused them to fire. Items to consider when using INSTEAD OF triggers include:
Triggers use two special database objects, INSERTED and DELETED, to access rows affected by the database actions. Within the scope of a trigger the INSERTED and DELETE objects have the same columns as the trigger’s table.
The INSERTED table contains all the new values; whereas, the DELETED table contains old values. Here is how the tables are used:
A trigger is defined for a specific table and one or more events. In most database management systems you can only define one trigger per table.
Below is an example trigger from the AdventureWorks2012 database.
You’ll notice the syntax for a trigger is very similar to that of a stored procedure. In fact, the trigger uses the same language to implement its logic as do stored procedures. In MS SQL, this is T-SQL; whereas in Oracle it is PL/SQL.
Here are some important parts to a trigger:
Here are some common uses for triggers:
You can use triggers to track changes made to tables. In our example above, changes made to the WorkOrder table are recorded a TransactionHistory table.
Typically when creating audit trails, you’ll use AFTER triggers.
You may think this is redundant, as many changes are logged in the databases journals, but the logs are meant for database recovery and aren’t easily accessible by user programs. The TransactionHistory table is easily referenced and can be incorporated into end user reports.
Triggers can be used to inspect all data before a DML action is performed. You can use INSTEAD OF triggers to “intercept” the pending DML operation, apply any business rules, and ultimately complete the transaction.
An example business rule may be that a customer status is defined as:
An INSTEAD OF trigger could be defined to check the customer status each time a customer record is added or modified. The status check would involve creating a sum of all the customers’ purchases and ensuring the new status corresponds with the sum of the last 12 months of purchases.
Triggers can be used to calculate column values. For instance, for each customer you may wish to maintain a TotalSales column on the customer record. Of course, for this to remain accurate, it would have to be update every time a sales was made.
This could be done using an AFTER trigger on INSERT, UPDATE, and DELETE statements for the Sales table.
In general, my advice is to avoid using triggers unless absolutely necessary.
You should avoid using triggers in place of built in features. For instance, rather than rely on triggers to enforce referential integrity, you’re better off using relationships.
Here are some reasons why I shy away from them:
标签:
原文地址:http://www.cnblogs.com/hellohelloworld/p/5628048.html