Database Operations-triggers, databases-triggers

Source: Internet
Author: User

Database Operations-triggers, databases-triggers

A trigger is a special type of stored procedure. Unlike a stored procedure, a trigger is triggered by an event and automatically called for execution, the stored procedure can be called by the name of the stored procedure.

Trigger

1. after trigger

2. insert trigger

3. update trigger

4. delete trigger

A trigger is a stored procedure that is automatically executed when a table is inserted, updated, or deleted. Although the trigger functions are powerful and easy to implement many complex functions, too many triggers may make it difficult to maintain databases and applications, and excessively dependent on the trigger, will affect the structure of the database, while adding a complex program for maintenance.

I learned how to use the trigger in the niugu news publishing system. When deleting the data in the table, I first Delete the data in other tables and then delete the data in the table.

 

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> USE [newssystem] GO/****** Object: Trigger [dbo]. [trigCategoryDelete] Script Date: 22:48:11 *****/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO -- ================ =================================-- Author: ma shichao -- Create date: 2015-2-4 -- Description: delete A category trigger -- ========================================== ========= alter trigger [dbo]. [trigCategoryDelete] ON [dbo]. [category] instead of deleteAS BEGINdeclare @ caId intselect @ caId = id from deleted -- delete comment where newsId in (select newsId from news where caId = @ caId) -- delete news delete where caid = @ caId -- delete category where id = @ caIdEND </span>
With this trigger, it is found that the trigger is not as difficult as you think as the view and stored procedure. The trigger should have a greater role. Now it is just a simple understanding. I believe it will play a greater role in the future.

Related Article

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.