EDIT: I don't know what distro it is, it's in an exam paper.
I'm just not getting this, sadly. I'm quite happy with Row level triggers but could someone explain to me how the results would differ if the trigger was statement level instead?
Relation/Statement Trigger/Row Level Trigger
Employee(ID VARCHAR2(30), Salary NUMBER)
Create Trigger AutoRaise
After insert on Employee
Referencing new table as NT
update Employee
Set salary = salary + (select avg(salary) from NT)
Create trigger AutoRaise
After insert on Employee
Referencing new table as NT
For each Row
Update employee
Set salary = salary + (select avg(salary) from NT)
I understand that in the for each row trigger it'll fire for each row affected by the triggering statement. Now would the statement level trigger modify the results differently? Say if I inserted five tuples in one statement, would it set the salary etc for them all? If so, what's the benefit of the row level trigger here?
I've tried searching but I just can't get my head around it.
Thanks,
EDIT: Now, I'm just being dense but would either trigger produce different outputs? For the statement level trigger if I used the example values:
In table before trigger's creation:
(A,50)
Added in ONE statement after trigger is created:
(B,70), (C,30)
The first trigger would set the salary for each tuple being inserted, surely? So the first would become 120 (as the average is 50, 70 + 50 = 120) and the second would become 80. If this is true, how does the second trigger differ in results?
The FOR EACH ROW option determines whether the trigger is a row trigger or a statement trigger. If you specify FOR EACH ROW , then the trigger fires once for each row of the table that is affected by the triggering statement.
For example, if a DELETE statement deletes several rows from a table, a statement-level DELETE trigger is fired only once, regardless of how many rows are deleted from the table.
A row-level trigger fires once for each row that is affected by a triggering event. For example, if deletion is defined as a triggering event for a particular table, and a single DELETE statement deletes five rows from that table, the trigger fires five times, once for each row.
The difference is that in case of statement level trigger SELECT avg(salary) FROM NT
will return average of salary for inserted rows, but in case of row level, avg(salary)
always equals to salary
of new record (trigger executed for each row individually). Also, statement level trigger will be executed if no records affected. In case of row level trigger most RDMS don't fire it when 0 records affected.
Side note. I believe the trigger bodies in the question are given for example only; otherwise, I'd recommend not using recursion in triggers even if particular RDMS has such an option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With