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 study SQLServer will certainly understand it. My understanding: when giving him an event, you can complete another event at the same time.
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 giving him an event, you can complete another event at the same time.
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:
<赵亚盟>
-- Create date: <August 10, 2014 21:25:06> -- Description:
<当插入一条充值记录时,自动更新卡表中的余额>
-- ===================================================== ====== 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.