First school trigger

xiaoxiao2021-03-06  51

Here is only to explain the four parts, the simplest, most commonly used four parts.

1, trigger.

Definition: What is a trigger? In the SQL Server is a certain manner to trigger a certain condition to trigger a certain condition. The trigger is a special stored procedure. There are three common triggers: applied to INSERT, UPDATE, and DELETE events, respectively. (SQL Server 2000 defines new triggers, not mentioned here)

Why should I use a trigger? For example, two tables:

Create Table Student (- Student Table StudentId Int Primary Key, - Study No. ....)

Create Table BorrowRecord (- Student Borrowing Record Table BorrowRecord Int Id "

The functions used: 1. If I change the students' student number, I hope that his borrowing record is still related to this student (that is, change the student number of the borrowing record); 2. If the student has graduated I hope to delete his student number and delete its borrowing record. and many more.

This can be used at this time. For 1. Create a UPDATE trigger:

Create Trigger Trustudent On Student for Update As if Update (Studient) Begin

Update borrowrecord set studentid = i.studentid from BorrowRecord Br, Deleted D, Inserted I where br.studentid = D.studentID

End understands two temporary tables inside the trigger: deleted, inserted. Note that deleted and inserted indicate the table "old record" and "new record" of the trigger event. A UPDATE process can be seen as: generating a new record to the Inserted table, copy the old record to the deleted table, then delete the Student record and write a new record.

For 2, create a delete trigger Create Trigger trdstudent on student for delete as delete BorrowRecord from BorrowRecord Br, Delted D Where br.studentId = D.studentID

From these two examples we can see the key to the trigger: A.2 temporary table; b. Trigger mechanism. Here we only explain the simplest trigger. Complex rendering. In fact, I don't encourage the use of triggers. The initial design idea of ​​the trigger has been replaced by the "Cascade"

转载请注明原文地址:https://www.9cbs.com/read-112013.html

New Post(0)