T-SQL (6) trigger)

Source: Internet
Author: User
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)

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.