What's a trigger? In the Oracle world, a trigger is an important system-level component that allows you to associate a "block of code" (usually PL/SQL or Java) with a specific system event, namely DML (SQL insert, update and delete) statements.
Oracle defines triggers as "procedures that are stored in the database and implicitly run, or fired, when something happens.What is the "something" that happens to make a trigger fire? There are 12 envents that can cause triggers to fire: Before/After during Insert/Update/Delete on a Row/Table.
Triggers are a special PL/SQL construct similar to procedures. However, a procedure is executed explicitly from another block via a procedure call, while a trigger is executed implicitly whenever the triggering event happens. The triggering event is either a INSERT, DELETE, or UPDATE command. The timing can be either BEFORE or AFTER. The trigger can be either row-level or statement-level, where the former fires once for each row affected by the triggering statement and the latter fires once for the whole statement.
Trigger Type
- BEFORE INSERT
- AFTER INSERT
- BEFORE UPDATE
- AFTER UPDATE
- BEFORE DELETE
- AFTER DELETE
Syntax
CREATE or REPLACE TRIGGER trigger_name BEFORE INSERT OR UPDATE OR DELETE ON table_name [ FOR EACH ROW ] DECLARE -- variable declarations BEGIN -- trigger code EXCEPTION WHEN ... -- exception handling END;
Examples
1. CREATE OR REPLACE TRIGGER orders_before_insert BEFORE INSERT OR UPDATE OR DELETE ON orders FOR EACH ROW DECLARE v_username varchar2(10); BEGIN -- Find username of person performing INSERT into table SELECT user INTO v_username FROM dual; -- Update create_date field to current system date :new.create_date := sysdate; -- Update created_by field to the username of the person performing the INSERT :new.created_by := v_username; END;
2. CREATE OR REPLACE TRIGGER orders_after_update AFTER UPDATE OR UPDATE OR DELETE ON orders FOR EACH ROW DECLARE v_username varchar2(10); BEGIN -- Find username of person performing UPDATE into table SELECT user INTO v_username FROM dual; -- Insert record into audit table INSERT INTO orders_audit ( order_id, quantity_before, quantity_after, username ) VALUES ( :new.order_id, :old.quantity, :new.quantity, v_username ); END;
Drop a Triggers
If you had a trigger called orders_before_insert, you could drop it with the following command.
DROP TRIGGER orders_before_insert;
Disable/Enable Triggers
If you had a trigger called orders_before_insert, you could Disable/Enable it with the following commands.
ALTER TRIGGER orders_before_insert DISABLE; ALTER TRIGGER orders_before_insert ENABLE;
If you had a table called Orders and you wanted to Disable/Enable all triggers on this table, you could execute the following commands.
ALTER TABLE orders DISABLE ALL TRIGGERS; ALTER TABLE orders ENABLE ALL TRIGGERS;
Thanks for sharing this Oracle database triggers. It is really useful.
ReplyDeleteOracle Training | Oracle Training in Chennai | Oracle course in Chennai
An informative blog and it is very useful
ReplyDeleteOracle Training in Chennai
Oracle course in Chennai
Keep sharing this blog. it look like very attractive content ....
ReplyDeleteDOT NET Training in Chennai
DOT NET Training in Bangalore
.Net training in chennai
best dot net training institutes in bangalore
aws training in bangalore
Data Science Courses in Bangalore
DevOps Training in Bangalore
PHP Training in bangalore
spoken english classes in bangalore
DOT NET Course in Chennai
I like this one...more helpful information provided here.I am quite sure I will learn much new stuff right here! Good luck for the next!
ReplyDeleteOracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore
ReplyDeleteThis blog is really awesome. I learned lots of informations in your blog. Keep posting like this...
German Classes in Bangalore
German Language Course in Bangalore
German Language Course in Hyderabad
German Language Course in Delhi
German Language Classes in Pune
If Big Data is a job that you're dreaming of, then we, Infycle are with you to make your dream into reality. Infycle Technologies offers the best Big Data Course Chennai, with various levels of highly demanded software courses such as Java, Python, Hadoop, AWS, etc., in 100% hands-on practical training with specialized tutors in the field. Along with that, the pre-interviews will be given for the candidates, so that, they can face the interviews with complete knowledge. To know more, dial 7502633633 for more.
ReplyDeleteBest software training in chennai
Great post. keep sharing such a worthy information.
ReplyDeleteSalesforce Training in Chennai
This post is so interactive and informative.keep update more information...
ReplyDeleteccna Training in Tambaram
ccna course in Chennai
This comment has been removed by the author.
ReplyDeleteMindblowing blog very useful thanks
ReplyDeleteAngularJS Training in Velachery
Angularjs Training in Chennai
Thanks for sharing post.
ReplyDeleteSQL training in Pune