Writing Triggers

Embed Size (px)

Citation preview

  • 8/13/2019 Writing Triggers

    1/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:DATEitems(!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"lfile"

    b" #rite row trigger against ITEMStable, which calculates automatically the sum of all bought items using theunit 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 saleoperation) execute them against the same account 'Solution:given in salesro&tri''ers%s"lfile"

    c" &repare and load some test data for two different sales operations with several item positions and explore theresult of automatic trigger execution after each of them 'Solution:given in saleso!erations%s"lfile"

    d" *reate an audit table HISTfor 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"lfile"

    e" #rite some statement triggers against the two tables SALESand ITEMSwhich register in HISTtable the

    corresponding operations performed at the till and execute them under the same account 'Solution:given in

    salesstatementtri''ers%s"lfile"

    f" *hange item prices, (uantities, positions and sales summaries in the existing sales operations 'Solution:given in

    salescan'es%s"l file". Analy!e and explain the generated messages.

    g" xecute the file salesmutatin'ta$les%s"lunder the same account and explore its content 'Hint: it contains

    row triggers which could cause table mutation when executed"

  • 8/13/2019 Writing Triggers

    2/2

    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 salescan'es%s"l"