Triggers in the database

Source: Internet
Author: User

Trigger

Step : Open the selected ' database ' and turn on ' programmability ' to find the ' database trigger '.

is a stored procedure, but does not execute through EXEC calls!

The execution is triggered by adding or removing changes to the statement !

When you have a primary foreign key association, you can modify the associated information

(When you have a primary foreign key association, you can delete the primary key before deleting the foreign key)

CREATE TABLE New

(Code int,

Shu int)

Go

INSERT into new values (

INSERT into new values (2,3)

INSERT into new values (3,4)

INSERT into new values (4,5)

INSERT into new values (5,6)

Select *from New

drop table New

----------------------------------------------------

Create Trigger Tr_new_delete-- Create and name a trigger

on New-- Acting on New Table

for Delete--delete/insert/update-- Execute DELETE statement first , then insert the statement

As

INSERT into new values (11,11)-occupies the location of the deleted data

INSERT into new values (3,4)--the location of the newly inserted data

Go to this, the trigger is complete.

Delete from new where code=3

--trigger statement, can only be deleted and modified

Select *from New

For/after run go before running as

--Go Delete code=3 first, then as add code=11,code=3

--Drop Trigger Tr_new_delete

--------------------------------------------------

Create Trigger Tr_new_delete

On new

Instead of delete

As

INSERT into new values (11,11)

Go

Delete from new where code=2

SELECT * FROM New

--Do not execute DELETE statement at this time, only execute INSERT statement!

instead of Run only as the statement

----------------------------------------------

Alter TRIGGER DONGTAICHUFA

On teacher

Instead of delete

As

Begin

DECLARE @Tno varchar (20)

Set @Tno = (select Tno from deleted)

--deleted represents a virtual table ( Go behind the Delete statement)

Update teacher set Tname= ' recursion ' where [email protected]

End

Go

Delete from teacher where tno= ' 856 '

Select *from Teacher

---------------------------------------------

-- a trigger can handle a view, (Purpose: 1. Backup 2 When deleting data . operation View, when there is a primary foreign key association)

Create Trigger INSERTTT

On teacher

For insert

As

DECLARE @Tno varchar (19)

Set @Tno = (select Tno from inserted)

Update teacher set tsex= ' male ' where [email protected]-- Modify Table Contents

Go

Insert INTO teacher values

(' 789 ', ' Set ', ' Female ', ' 1990-09-09 ', ' Professor ', ' Biology Department ')

Select *from Teacher

----------------------------------------------

Alter table teacher Disable trigger all--( or trigger name ) Disable all

ALTER TABLE teacher enable trigger all--(or trigger name) all

Triggers in the database

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.