Lab #11 Worksheet
Lab #11 Worksheet
Lab #11 Worksheet
1. Triggers
A trigger is procedural SQL code associated with a given table, and is automatically
executed before or after some update/insertion/deletion on that table (triggering
event). A trigger can be used to enforce constraints, to automate critical actions (like
updating some derived attribute). A trigger can update values, insert records, and
call procedures. A table may have multiple triggers that are fired on different events
and can do different things. Triggers add processing capabilities to a database
application (event-based actions).
Second, bind the trigger function to a table by using CREATE TRIGGER statement.
A trigger function receives data about its calling environment through a special
structure called TriggerData which contains a set of local variables. For example,
OLD and NEW represent the states of the row in the table before or after the
triggering event.
CREATE TRIGGER trigger_name {BEFORE | AFTER} { event }
ON table_name
[FOR [EACH] { ROW | STATEMENT }]
EXECUTE PROCEDURE trigger_function
a) First add a new smallint attribute called reorder in the table Product. The new
attribute will have value 1 when the quantity on hand for a given product falls below
the minimum allowed quantity on hand.
UPDATE Product
SET reorder = 1
WHERE productCode = NEW.productCode;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
ii) Now pick a product that has reorder different than 1, and decrease its quantity on
hand so that it becomes less than its minQoH. For example:
UPDATE Product
SET quantityOnHand = quantityOnHand -9
WHERE productCode = '1546-QQ2';
What happened? Please note that this trigger may need improvement.
i) Add attribute amount of type decimal (8, 2) (not nullable) to table Invoice,
with a
ii) Update the amount attribute to be the sum of the corresponding total line
prices. You can use a correlated subquery. Give the corresponding SQL
statement.
iii) Create a trigger that will automatically update (increase) the invoice
amount every time a line is inserted (use the example trigger on the handout
online).
iv) Insert some invoice line for some existing invoice and check the effect on
the corresponding invoice.
c) Create a trigger that would update (increase) the customer balance every time an
amount is invoiced for that customer (example trigger on the handout online).
2. Cursors.
A cursor is a procedural SQL mechanism that allows for walking through the result
set of a query that may return more than one row. It is used to fetch the information
one record at a time. A cursor needs first to be declared; the basic syntax looks like:
You also need to declare local variables where to fetch data from cursor; for example
OPEN cursorName;
The special variable FOUND can be checked to see whether a row was obtained or
not.
LOOP
-- fetch next row and loop back to process it FETCH NEXT FROM
cursorName INTO variableName,...;
END LOOP;
CLOSE cursorName;
CALL spPrintCustByArea('0161');
NB. Please note that cursors can be used in SQL functions as well.