Application of Trigger in system

Source: Internet
Author: User
Tags getdate


Computer room charge system, in the card operation, to delete the data in the Student information table and to write the refund amount to the Return Card record table, two SQL statements can be implemented, but there is no simpler way, think of the database video to learn the trigger, one operation caused by other operations.

This part of the trigger is learned again.

A summary of the trigger, as the name implies is in the database operation caused by another operation, where the operation includes only (update delete inserte), the problem with triggers is to write (delete) data to a table, while the written information stored in another table, Alternatively, when you write (delete) The data, the data in the other table changes. These can all be implemented with triggers.


The following is the realization of the implementation of the trigger to implement the card operation.

According to the characteristics of the trigger, only need to set up a trigger in the deleted Student basic information table, write the statement written to the withdrawal record, in the student table, when the deletion, will automatically delete the record to write back card record, this realization is indeed simple ha.

Here's the problem. The Student basic information table structure and the withdrawal card record table structure is different, when writing, on the one hand will delete the information the card number, the balance, the Operator field content writes, but also obtains the system at that time, the date field writes. This is a problem.

Continue to find information, a lot of information on the Internet that "trigger operation of the table structure consistent", inconsistent words, it can not be used, just when I was about to give up the trigger, the implementation of the different table structure operation. The process is complex and the result is simple. The code is for reference only:

ALTER TRIGGER [dbo]. [Delstudent] 
   On  [dbo].[ T_student] for
   DELETE
as 
BEGIN
	inserts into T_carddelete (Cardno,deldata,deltime, Cardcash, Cardmanager values (select Cardno from Deleted), convert (varchar, GETDATE (),), convert (varchar), GETDATE (), 108), (select Cardcash from deleted), [select Stmanager from deleted)] End

Code Explanation:

(Cardno,deldata,deltime, Cardcash, Cardmanager) is the name of the field to insert in the table

Values are followed by the inserted content

(select Cardno from deleted) is the card number field that is obtained in the Delete table, and the card number field in the corresponding record table

CONVERT (varchar), GETDATE (), 103) Get the System date field, fill in the other fields, of course, if it's a fixed string, just write it, that's it.

Triggers can also be restrictive, such as prompting when a database is deleted, protecting raw data, and so on. These functions in the future to do the system reuse, first understand.

Triggers are easy to use, but they can't be abused. When the data is particularly high, triggers are not suitable.


Description: For=after is triggered only after execution.

Instead of instead of SQL statements, executing triggers rather than previous SQL statements




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.