Use of trigger for "computer room toll System"

Source: Internet
Author: User

In the machine room reconfiguration, there are many functions of the implementation of the database in the operation of more than one table. For example, checkout, after the successful checkout function is: 1, modify the card table checkout status. 2. Revise the checkout status of the Recharge list. 3. Modify the checkout status of the return card form. 4. Delete the card information that is not used in the card table and has been checked out. It is not difficult to implement these functions, as long as you redefine these methods in the D layer, but this is not very efficient. Is there an efficient way to achieve these functions? This involves the use of triggers in the database.

trigger

A: A trigger is a special kind of stored procedure that cannot be called explicitly, but is automatically activated when a record is inserted into a table, a record is updated, or a record is deleted. So triggers can be used to implement complex integrity constraints on a table.


Two:sql server created two dedicated table:inserted tables and deleted tables for each trigger. These two tables are maintained by the system, and they exist in memory rather than in the database. The structure of the two tables is always the same as the structure of the table that the trigger acts on. The two tables associated with the trigger are also deleted after the trigger execution is complete. The deleted table holds all rows that are to be removed from the table because of execution of the DELETE or UPDATE statement. The inserted table holds all rows to be inserted into the table because of an INSERT or UPDATE statement execution.


Three: There are two common types of triggers: After (for), instead of, for INSERT, UPDATE, delete events. after (for) executes the trigger afterexecuting the code,and instead of means to replace your action with the already written trigger before executing the code.

Four: Grammar    

Create trigger trigger name on   action table For|after         instead of Update|insert|delete as SQL statement
here is a trigger for the checkout function, which will not only help me implement the function, but also avoid writing too much code in the D layer!
Application:

CREATE TRIGGER triggerc_updatebon card_info after  update as if update ([ICheck]) Update returncard_info set icheck= ' Closed Account ' where icheck= ' not checkout ' and userid= (select top 1  UserID from  INSERTED) update recharge_info set icheck= ' checkout ' where IC heck= ' not Checkout ' and userid= (select top 1  UserID from  INSERTED) Delete from card_info where icheck= ' checkout ' and ustate= ' do not make With
Summary:

before learning the database, I learned about the trigger. There was no application of this knowledge, and refactoring was used to discover that its functions were so powerful. Sure enough, practice is the truth.




Use of trigger for "computer room toll System"

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.