Examples On Triggers: Instructor: Mohamed Eltabakh Meltabakh@cs - Wpi.edu
Examples On Triggers: Instructor: Mohamed Eltabakh Meltabakh@cs - Wpi.edu
Examples On Triggers: Instructor: Mohamed Eltabakh Meltabakh@cs - Wpi.edu
1
Example 1
If the employee salary increased by more than 10%, make sure the
‘rank’ field is not empty and its value has changed, otherwise reject the
update
2
Example 2
If the employee salary increased by more than 10%, then increment the
rank field by 1.
3
Example 3: Using Temp Variable
If the newly inserted record in employee has null hireDate field, fill it in
with the current date
4
Example 4: Maintenance of
Derived Attributes
Keep the bonus attribute in Employee table always 3% of the salary
attribute
5
Combining Multiple Events in
One Trigger
l If you combine multiple operations
l Sometimes you need to know what is the current operation
8
Example 5: Statement-level
Trigger
Store the count of employees having salary > 100,000 in table R
9
Order Of Trigger Firing
10
Some Other Operations
l Dropping Trigger
SQL> Drop Trigger <trigger name>;
11
Example on Triggers
12
Part 1
Sum of loans taken by a customer
does not exceed 100,000
Select sum(amount) into sumLoan Get the sum of loans for this customer
From loan L, Borrower B
where L.loan_number = B.loan_number
And B.customer_name = custName;
For each table, create “Before Update” trigger preventing a change on PK columns
15