Special stored Procedures-Triggers Overview _ database Other

Source: Internet
Author: User
This section describes a special stored procedure, that is, a trigger.

First, the concept of triggers
A trigger is a special type of stored procedure that differs from the stored procedure we described earlier. Triggers are executed primarily by triggering events, and stored procedures can be invoked directly by stored procedure names. When you perform actions such as update, INSERT, and delete on a table, SQL Server automatically executes the SQL statements defined by the trigger, ensuring that the processing of the data must conform to the rules defined by those SQL statements.

Second, the role of triggers
The main function of a trigger is its ability to achieve complex referential integrity and data consistency that cannot be guaranteed by primary and foreign keys. In addition, triggers have many different functions:

A strengthen constraints
Triggers can implement constraints that are more complex than check statements.

B Tracking changes
Triggers can detect operations within a database and do not allow unauthorized updates and changes in the database.

C Cascade Run
Triggers can detect operations within a database and automatically cascade the contents of an entire database. For example, a trigger on a table contains data operations on another table (such as delete, UPDATE, insert), which causes triggers on the table to be triggered.

D Calls to Stored procedures

In response to database updates, triggers can invoke one or more stored procedures, even outside the DBMS itself, through calls to external procedures.

This shows that triggers can solve some of the problems of high-level business rules or complex behavior restrictions and the implementation of custom records. For example, triggers can find differences in the state of a table before and after the data is modified, and perform certain processing according to the difference. In addition, multiple triggers for the same type of table (INSERT, UPDATE, DELETE) can take a variety of different processing for the same data operation.

Generally speaking, trigger performance is usually low.

Iii. types of Triggers
SQL Server 2000 supports two types of triggers: after triggers and instead OF triggers.

After triggers require that only an action (INSERT, UPDATE, DELETE) is performed, the trigger is triggered and can only be defined on the table. You can define multiple triggers for the same operation on a table.

INSTEAD of triggers represent actions that do not perform their defined operations (INSERT, UPDATE, DELETE), but only execute the trigger itself. You can define a instead of trigger on a table or a instead of trigger on a view, but you can define only one instead of trigger for the same operation.

NOTE: Instead OF triggers are not spoken in this experiment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.