16, SQL Basic collation (trigger. Easy Backup)

Source: Internet
Author: User

Trigger (Easy Backup)

is essentially a stored procedure, but not through exec to invoke execution, but by adding and deleting the operation of the database to execute (can manipulate the view)

Disable all triggers

ALTER TABLE teacher disable trigger All

Turn on Trigger all

ALTER TABLE teacher enable trigger all

Create TRIGGER tr_student_delete--Default trigger name

On student--which table to operate on

instead of Delete--for what to do with the table, ' for ' is used after the external statement is executed, ' instead of ' is the statement to be executed directly to execute as inside the statement

As

Delete from score where Sno =108

Delete from student where Sno = 108

Go

Delete from student where Sno = 108

Select *from Student

108 lines are erased.

ALTER TRIGGER tr_student_delete--DEFAULT trigger name

On student--which table to operate on

for Delete

As

INSERT into student values (108, ' Peng Zeng ', ' Male ', 1905-05-22,95033)

Go

Delete from student where Sno = 108

Select *from Student

* Zeng Hua has become a Peng Zeng

Dynamic triggering

Create Trigger Dongtaichufa

On teacher

Instead of delete

As

Begin

DECLARE @tno varchar (20)

Set @tno = (select TNO from Deleted)--select TNO in the execution statement @tno

Update teacher set Tname= ' Zhang San ' where TNO = @tno

End

Go

Delete from teacher where tno= ' 831 '

deleted Store the deleted information, inserted store the changed information.

Execution Delete statement when the tno= inside the trigger. TNO , but not @tno

Create Trigger Tr_teacher_insert

On teacher

For insert

As

Begin

DECLARE @tno varchar (20)

Set @tno = (select TNO from inserted)

---delete from teacher where [email protected]

DECLARE @sex varchar (10)

Set @sex = (select Tsex from teacher where [email protected])

if (@sex = ' male ')

Update teacher set tsex= ' female ' where TNO [email protected]

Else

Update teacher set tsex= ' man ' where TNO [email protected]

End

Go

Insert into teacher values (800, ' Harry ', ' Male ', 1990-09-09, ' Professor ', ' Computer Department ')

16, SQL Basic collation (trigger. Easy Backup)

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.