

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This chapter explains the concept of database triggers, their structure consisting of event, precondition, and action. Triggers are used for constraint maintenance, enforcing business rules, monitoring, and maintaining auxiliary data. Triggers can be invoked immediately or deferred, and their granularity can be row-based or statement-based. Examples of triggers for 'on delete cascade' and 'on update cascade' using pseudocode.
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Chapter 7 A trigger is an element of the database schema that has the following structure: On event If precondition Then action. Event is a request for the execution of a particular database operation (insert, delete, etc.) Precondition is an expression that evaluates to true or false. Action is a statement of what needs to be done when the trigger is fired. Trigger uses: Constraint maintenance. Enforce business rules. Monitoring. (sensor based systems) Maintenance of auxiliary cached data. For example, materialized views. Separate core program logic from exception handling. Example (in pseudocode): On inserting a row in course registration table If over course capacity Then abort registration transaction Two time when triggers can be invoked: Immediately when the triggering event is requested. Deferred until after the transaction commites. Also, when triggers are considered immediately their action may be executed immediately or it may be deferred until the end of the transaction. Also, when triggers are considered immediately the trigger can be executed after the triggering event is carried out, before the triggering event is carried out, or instead of the triggering event. Trigger Granularity: Trigger invoked on change to a row or when a statement is executed. For example, think of on delete cascade as a trigger. Exam Question #3: Contrive a database design with two tables, one table with a foreign key referencing the other table. Supply create table statements for those two tables. Supply pseudocode for triggers which implement 1.“on delete cascade”
Before insert on transcript Referencing new as n For each row When ((select count(t.studid) from transcript t where t.crscode=n.crscode and t.semester=n.semester)
= (select l.limit from crslimits l where l.crscode=n.crscode and l.semester=n.semester)) rollback Example 7.3.2 (Business rule enforced with an AFTER trigger) Create trigger limitsalaryraose After update of salary on employee Referencing old as o New as n For each row When (n.salary-d.salary>0.05+salary) Update employee set salary = 1.05*o.salary Where id =o.id Example 7.3.3 (Statement-level trigger) Create trigger recordnewaverage After update of salary on employee For each statement Insert into log Values (current_date, (select avg(salary) from employee))