Data center charging system-triggers and data center charging system triggers

Source: Internet
Author: User

Data center charging system-triggers and data center charging system triggers

In the previous article, we learned how to use the view. We still need to know a lot about the database.

What is a trigger? How to use it? What are the advantages of using it? There will certainly be a series of drawbacks, and so on. Many people who learn SQL Server will certainly understand it.

 

My understanding:

When an event is given to him, another event is completed at the same time. In the data center reconstruction, When I recharge the recharge table, I will give it an Insert event. The trigger can be used to refine the balance in the card table at the same time.

 

1. What to do:

A trigger is a method that SQL Server provides to programmers and data analysts to ensure data integrity. It is a special stored procedure related to table events. The trigger cannot be executed directly. It can only be triggered by the Insert \ Update \ Delete event on the table. Nor can it pass or accept parameters.

2. How to use:

We have introduced that triggers are triggered by adding, deleting, modifying, and modifying events on the table. Therefore, you must create a trigger in the table that requires the trigger.

Take the recharge table in the data room charging system as an example

-- ===================================================== ====== -- Author: <Zhao yameng> -- Create date: <August 10, 2014 21:25:06> -- Description: <when a recharge record is inserted, automatically update the balance in the card table> -- ================================== ============= alter trigger [dbo]. [CardBalance] ON [dbo]. [T_Recharge] for InsertAS declare @ AddMoney varchar (8) declare @ Balance numeric (18, 0) declare @ CardNo varchar (8) -- declare the parameter select @ AddMoney = Addmoney from T_Recharge select @ Balance = Balance from T_Card select @ CardNo = CardNo from T_Recharge -- assign the parameter BEGINUPDATE T_Card set Balance = @ Balance + @ AddMoney Where T_Card. cardNo = @ CardNo END

Explanation of code functions:

I have two tables. The recharge table stores the recharge records and the balance in the card table. When I recharge the table, I insert a record into the recharge table to update the balance in the card table at the same time. Current Balance in the card table = last balance + Recharge Amount


3. advantages of using triggers:

1. The trigger overhead is very low. The time used to run the trigger is mainly used to reference other tables stored in memory or disk.

2. Cascade changes using the relevant tables in the database.

3. Data integrity is more complex than CHECK constraints.

 

Disadvantages:

No matter what it is, triggers cannot be abused. They also have many disadvantages. Misuse of triggers can cause maintenance difficulties for databases and applications. If we rely too much on triggers, it will definitely affect the database structure.


The first time I tried to use a trigger, I felt very convenient and reduced the Code a lot. I had to study the use of the trigger in the future, and I had to explore the strength of the database.





IDC charging system source code

Add me to help you

C language student room charging system notice

The options are printed using printf, with \ t \ n and so on;
Enter the menu number. This is your parameter entry. Use scanf;

Next, you must use a switch for case selection;

In this way, beginners may not be puzzled,
As a person who has been here, I must give a suggestion: I must lay the foundation well. This is like Zhang Wuji practiced jiuyang's power, and then practiced everything quickly. internal strength is a basic skill and internal strength is good, it's up to you to be tempted, changing, but you can't leave it alone;

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.