Without knowing the trigger, we usually seldom come into contact with the trigger, and the trigger operation is generally done by the DB staff. However, sometimes some simple businesses need to be done by ourselves, so we cannot bother the database staff every time. Therefore, programmers need to be full of talent and be involved in the professional knowledge they think about programming, for example, js, css, html, and t-SQL
Without knowing the trigger, we usually seldom come into contact with the trigger, and the trigger operation is generally done by the DB staff. However, sometimes some simple businesses need to be done by ourselves, so we cannot bother the database staff every time. Therefore, programmers need to be full of talent and be involved in the professional knowledge they think about programming, for example, js, css, html, and t-SQL
Without knowing the trigger, we usually seldom come into contact with the trigger, and the trigger operation is generally done by the DB staff.
However, sometimes some simple businesses need to be done by ourselves, so we cannot bother the database staff every time. Therefore, programmers need to be full of talent and be involved in the professional knowledge they think about programming, for example, JavaScript, css, html, t-SQL, and other syntaxes may not be proficient, but you must be familiar with them. At least the syntax can be understood. This will have a multiplier effect on our programming, if an exception occurs, we can debug it easily to identify the error as soon as possible.
Let's get down to the truth. What is the trigger, as the name suggests, is that if you do an operation, it will trigger another event and perform some operations.
For example, if you click fireworks, the click is an action, and the fireworks is another action, the click operation will trigger the fireworks action.
In addition, the trigger must be attached to a subject, such as a table, just like the concept of events in programming.
Next we will use a simple example to understand and use the trigger step by step.
Instance requirements:
1. Create a product table (Store), an order table (orders), and a log table (Logs)
2. Create an order table insert trigger to insert an order information. The number of items in the item table decreases accordingly, and the total amount in the order table increases accordingly.
3. Create an order table update trigger to update an order information. The number of items in the item table changes accordingly, and the total amount in the order changes accordingly. (Similar to 2)
4. Create a log table trigger to update the price changes of the product table.
1. Create a product table (Store), an order table (orders), and a log table (Logs)
The first step is to create a table and add some sample data.
Sample Code:
Store 2 (, 6 ProductCH nvarchar (80) not null,); orders11 (,) Logs19 (, 21 operatedatetime, 22 ProductID int, 23 oldprice money, 24 newprice money25); dbo. store, 50) dbo. store, 50) dbo. orders (OrderID, ProductID, BuyNumber, buyprrn) 32 values)