Skip to main content

Creating triggers

Triggers are SQL statements which are stored with the intention of being activated or fired when an event associated with a database table occurs. This could be any event including an INSERT, UPDATE and DELETE. Lets begin by creating a few simple insert triggers

  1. CREATE a trigger on the ORDERLINE table for the insert event. The trigger will update the corresponding PRODUCT record QTY_ORDERED by adding the value of QTY to QTY_ORDERED.
  2. CREATE TRIGGER tr_qty_ordered_value_insert 
    ON Orderline FOR INSERT
    AS
    BEGIN 
         UPDATE product 
          SET QTY_ORDERED =  QTY_ORDERED +
     ((SELECT qty from INSERTED) * (SELECT unitprice from INSERTED)) 
           WHERE product.ProductID = (SELECT ProductID from INSERTED);      
    END;
    Command(s) completed successfully.
    
  3. CREATE a trigger on the ORDERLINE table for the delete event. The trigger will update the corresponding PRODUCT record QTY_ORDERED by subtracting the value of QTY FROM QTY_ORDERED.
  4. CREATE TRIGGER tr_qty_ordered_value_delete 
    ON Orderline FOR DELETE
    AS
    BEGIN 
         UPDATE product 
          SET product.QTY_ORDERED =  product.QTY_ORDERED -
     ((SELECT OrderLine.qty from orderline) * 
      (SELECT OrderLine.unitprice from orderline)) 
           WHERE product.ProductID = (SELECT ProductID from orderline);          
    END;
    
    Command(s) completed successfully.
    
  5. CREATE a trigger on the ORDERLINE table for the update event. The trigger will update the corresponding PRODUCT record QTY_ORDERED by subtracting the OLD QTY and then adding the NEW QTY
  6. CREATE TRIGGER tr_qty_ordered_value_update
    ON Orderline FOR UPDATE
    AS
    BEGIN 
         UPDATE product
         SET QTY_ORDERED =  QTY_ORDERED -
          ((SELECT qty from DELETED) * (SELECT unitprice FROM DELETED))
                +
          ((SELECT qty FROM INSERTED) * (SELECT unitprice FROM INSERTED))
          WHERE product.ProductID = (SELECT ProductID from INSERTED);
    END;
    
     Command(s) completed successfully.
    

Comments

Popular posts from this blog

Router modes and channel impairments

What does it mean for a wireless network to be operating in "infrastructure mode?" Clients connected to a base station (an access point or router) are operating in infrastructure mode. They communicate indirectly through this access point or router which serves as a bridge to a wired network. Ad-hoc mode networks are networks that do not rely on a router or access point infrastructure. As a result, each client participates in the routing by forwarding data to the other connected clients. What are the differences between the following types of wireless channel impairments:  path loss, multipath propagation, interference from other sources? Multipath propagation is when packet loss occurs due to electromagnetic waves reflecting off of ground objects which then take paths of different lengths between sender and receiver. Interference from other sources happens when there is interference from radio sources transmitting in the same frequency band. Wireless phones and wirel...

Maine summer 2014 photo shoot

Wells Maine beach drive I made a trip up to Wells-Ogunquit Maine last year for my vacation. I was fascinated with the coastal rock formations along the beaches that other photographers had posted throughout the web. I wanted to try my hand at capturing the dawn light and the sunlight reflecting off of the rocks with a long exposure time. In addition, some much needed away time to ride my bike and read. Marginal Way Day one consisted of scouting locations. Perhaps a quick bike ride or beach outing followed by a lobster sandwich. After unpacking I took my bike out through the streets of Wells Maine. I was struck by how similar it was to the town in the movie Jaws Martha's Vineyard. Wells is a small town with narrow streets and older homes. The beach drive featured a fantastic stretch of road just perfect for biking. After a great seafood dinner I scouted the Marginal way.  A combination of site seeing and shooting with the iPhone.  I really underestimated the amount ...

Database Normalization Basics

There are 3 primary reasons why a database should be normalized: It brings data integrity to the tables (entities). This facilitates proper inserting, updating, and deleting of data thereby preventing data anomalies. If a table is not normalized it runs the risk of inconsistent data, data redundancies, and inconsistent result sets.   To create entity relationships thereby providing referential integrity. Referential integrity ensures that relationships between tables remain consistent when entities are altered. To avoid having one set of users with a biased view of the data. For example a marketing department may view the data in line with their revenue needs rather than having an objective view of the data as it applies to the entire organization. We begin by analyzing the current relationships that exist between the attributes within each entity. Apparent in the Orders entity is the repeating data in the ITEM_NAME, ITEM_DESCRIPT, QUANTITY, and PRICE attributes. We bre...