You are on page 1of 2

Writing Triggers

1. Scenario
Typical component of any sales system used in supermarket chains is a database schema for registering customer sales. Suppose the following two relations are used to represent the sales operations and the items sold in each of them:
sales(invNo:INTEGER, customer:STRING, items:INTEGER, total:MONEY, sale_date:DATE items(!osNo:INTEGER, invno:INTEGER, name:STRING, !rice:MONEY, "t#:INTEGER, sum:MONEY

Assuming most of the calculations done during customer sales are implemented using database triggers, in this seminar you should write and experiment with various triggers for automation of certain typical operations for sales.

2. Aims of this seminar


To practice writing of row and statement triggers against database tables using standard SQ and to explore the effect of their execution using interactive database manipulation To analy!e the reasons for table mutation when using database triggers and the restrictions for avoiding them

3. Tasks
a" #rite SQ $$ statements for creating database tables which implement the above database schema and execute them using SQ %&lus under your database account 'Solution: given in salesdata$ase%s"l file"

b" #rite row trigger against ITEMS table, which calculates automatically the sum of all bought items using the unit price and the (uantity for each of them, and another statement trigger against the above tables which automatically updates the total sum and the number of different positions bought during the current sale operation) execute them against the same account 'Solution: given in salesro&tri''ers%s"l file" c" &repare and load some test data for two different sales operations with several item positions and explore the result of automatic trigger execution after each of them 'Solution: given in saleso!erations%s"l file"

d" *reate an audit table HIST for registering different possible sales operations performed typically by the cashiers at the till according to the following specification:
(ist(no:INTEGER,ta$_name:STRING,s"l_o!:)*AR,dat:DATE,col_name:STRING,count:INTEGER

where s"l_o! can have one of the two possible values + +,+ for updating of prices and (uantities and +D+ for deleting of some of the positions in the current sales operation or canceling the entire operation ' Solution: given in salesaudit%s"l file" e" #rite some statement triggers against the two tables SALES and ITEMS which register in HIST table the corresponding operations performed at the till and execute them under the same account 'Solution: given in salesstatementtri''ers%s"l file" *hange item prices, (uantities, positions and sales summaries in the existing sales operations ' Solution: given in salesc(an'es%s"l file". Analy!e and explain the generated messages.

f"

g" ,xecute the file salesmutatin'ta$les%s"l under the same account and explore its content 'Hint: it contains row triggers which could cause table mutation when executed"

h" -un some operations for changing prices, (uantities, positions and sales summaries and analy!e the generated messages 'Hint: .ou may wish to prepare your own file similar to salesc(an'es%s"l"

You might also like