"SQL"--Trigger

Source: Internet
Author: User

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

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.