Concept
A trigger (trigger) is a method that SQL Server provides to programmers and data analysts to ensure data integrity, which is a special stored procedure related to table events, which is executed either by a program call or by a manual startup, but by an event, such as when a table is manipulated ( Insert,delete, update) will be activated when it executes. Triggers are often used to enforce data integrity constraints, business rules, and so on. --Baidu Encyclopedia
In fact, a trigger is similar to a programming language event, which is caused by something else ... It is similar to a stored procedure, which can implement a series of operations quickly and completely.
Example
--=============================================--Author: Zhaohan--Create date:2015-6-13 16:51:25--Description: Delete Category trigger--=============================================create TRIGGER [dbo]. [Trigcategorydelete] on [dbo]. [CategoryInfo] Instead of delete --replace delete operation as Begindeclare @caId intSELECT @caId =id from deleted --category number to delete--delete comment Delete Commentinfo where NewsId in (select NewsId from newsinfo where [email protected])--delete news delete newsinfo where [email protect ed]--Delete a category delete categoryinfo where [email protected]end
to remove a category from the news release system, you need to delete it along with the news content in the category and the comments on the news content. Because they have the primary foreign key relationship, so when you want to delete the category, in fact, the deletion order should be: comments, news content, category. Therefore, you cannot trigger an event in a trigger by deleting the information, so use:
Instead of DELETE
To replace. This means replacing the above mentioned delete event with the following event.
So, the meaning of the above trigger is:
This trigger is triggered when a category is deleted, but is not deleted and replaced by the following three delete statements.
At this point, the information you want to delete is already in deleted. Therefore, the class alias is selected from deleted.
Find the news number through the category number, find the comment number by the news number, and delete the information you want to delete in the order of comments-news-categories.
in this way, a single trigger, a series of execution is done, to ensure the integrity of the data, and very efficient ~ ~
"SQL"--Trigger