Introduction
Suppose we are updating the marks in the STUDENT database for some of the students after re-evaluation. When we are updating the marks, their old values are lost. But in general view it is better to keep the old marks too – just to know what was the mark before and what is now. But we do not have any columns to store old marks in the MARKS table. We have to have a separate table to store the update log for marks updates. But this cannot be done in single MARKS table update. In this case, we need to update MARKS table as well as we need to insert an entry in the MARKS_LOG table. Both are different transactions.
What can be done in this case? We can write a procedure to update and insert and call it every time we have to update marks. But main purpose of the person who is updating the mark is only to update the marks. But inserting into log table is secondary for him or least important for him, but at the same it is an important to store that information somewhere. So he will use only UPDATE statement to correct the marks. In such case triggers are used to insert the record into log files.
Triggers are blocks of PL/SQL code, which are executed as a result of any insert / update / delete on the table. There will not be any explicit call for triggers. It is always automatically executed. It might perform single query or set of queries to meet the goal. But it will be always written as a PL/SQL block. It is always associated with insert / update / delete of a table. But the set of transaction inside the trigger can be performed on the same table or one or more other tables.
The general syntax of a trigger is as follows:
A typical trigger has 3 main components
CREATE OR REPLACETRIGGER tr_marks_log -- creates a trigger, if exists replaces it
AFTER UPDATE OF m.sub_mark ON MARKS – after updating MARKS table’s sub_mark column
FOR EACH ROW -- for each row update
BEGIN
INSERT INTO marks_log (STD_ID, SUBJECT, OLD_MARK, NEW_MARK)
VALUES (:old.std_id,:old. subject, :old.sub_mark, :new.sub_mark);
EXCEPTION
When OTHERS THEN
raise_application_error (-20015, ‘Error while inserting marks log’);
END;
Types of Triggers
There are two types of triggers.
In below example, once the Students are inserted, it calculates the age of the student and updates STUDENT table.
CREATE OR REPLACETRIGGER tr_calc_age
AFTER INSERT ON STUDENT
BEGIN
UPDATE STUDENT s
SET AGE = SYSDATE – s.DATE_OF_BIRTH
WHERE s.STD_ID =:old.STD_ID;
EXCEPTION
When OTHERS THEN
raise_application_error (-20015, ‘Error while inserting marks log’);
END;
In both the cases, the DML statement may be on particular column(s) or on whole table.
In addition to above types of trigger, we can have triggers which are called so because of the time when they are executed.
Some of the key points about trigger
Execution Order
We can have multiple types triggers on the same table. Since there is BEFORE, AFTER, INSTEAD OF, ROW level, STATEMENT level etc, there should be some order for it to execute. The order of execution of trigger is as follows.
Note: this is the order in which single trigger with different types will be executed.
Mutating tables
Suppose we have inserted some records into STUDENT table. We have created a row level trigger on the same table to compute the total number of records after the insertion. What is wrong in this situation? It looks perfect right? But it will throw a mutating table error. What happens in this case is, STUDENT table is inserted with records. The row level trigger will be fired after each row insertion. But transaction on STUDENT is still not complete and trigger is trying to access STUDENT. There will be INSERT lock on STUDENT which is not released for trigger to select the count. Hence trigger will be waiting for the insert to complete. But the insert is waiting for the trigger to complete to proceed for the next row insertion or to complete the transaction. Hence both will be waiting for each other to complete their transaction. Such a condition on the table is called mutating error.
Similarly, if there are two tables A and B, and two triggers where one of the trigger will be updating the table B when there is insertion on A and another trigger will be inserting on the table A when there is update on B. In this case both the tables will be waiting for each other. This is also another condition of mutating tables. It is a kind of deadlock situation when triggers are used.
Source: http://www.snscourseware.org/snsct/files/CW_588ecd4e188d1/triggers.docx
Web site to visit: http://www.snscourseware.org/
Author of the text: indicated on the source document of the above text
If you are the author of the text above and you not agree to share your knowledge for teaching, research, scholarship (for fair use as indicated in the United States copyrigh low) please send us an e-mail and we will remove your text quickly. Fair use is a limitation and exception to the exclusive right granted by copyright law to the author of a creative work. In United States copyright law, fair use is a doctrine that permits limited use of copyrighted material without acquiring permission from the rights holders. Examples of fair use include commentary, search engines, criticism, news reporting, research, teaching, library archiving and scholarship. It provides for the legal, unlicensed citation or incorporation of copyrighted material in another author's work under a four-factor balancing test. (source: http://en.wikipedia.org/wiki/Fair_use)
The information of medicine and health contained in the site are of a general nature and purpose which is purely informative and for this reason may not replace in any case, the council of a doctor or a qualified entity legally to the profession.
The texts are the property of their respective authors and we thank them for giving us the opportunity to share for free to students, teachers and users of the Web their texts will used only for illustrative educational and scientific purposes only.
All the information in our site are given for nonprofit educational purposes