7) trigger 1. Two basic concepts: implement the constraints of complex business rules that are difficult to complete due to database integrity constraints; monitor various database operations to implement audit functions. Triggers are divided into: DML triggers (triggered when DML operations are performed on tables or views), instead triggers (defined only on views, instead of actual operation statements ), system trigger (triggered when operating the database system, such as DDL statements, start or close the database): the brackets in the above trigger are all trigger events. Trigger condition: When clause trigger object: including table, view, mode, and database. Trigger operation: The program automatically executed by the trigger. Trigger time: the time when the trigger is executed relative to the operation. The condition predicate before/after: inserting (the trigger event is true when the trigger event is insert), updating, deleting trigger subtype: Row trigger and statement trigger, new and old tables in the trigger. 2. create or replace trigger <trigger Name> trigger condition trigger create trigger my_trigger -- Define a trigger my-trigger before insert or update of TID, tname on teachers for each row when (New. tname = 'David ') -- this part is the trigger condition declare -- the following part is the trigger body teacher_id teachers. TID % type; insert_exist_teacher exception; begin select TID into teacher_id from teachers where tname = new. tname; Raise insert_exist_teacher; Exception -- Exception Handling is also available here when in Sert_exist_teacher then insert into error (TID, err) values (teacher_id, 'The Teacher already exists! '); End my triqqer; 3. execute the trigger to automatically execute create trigger my_trigger1 after insert or update or delete on teachers for each row; declare info char (10); begin if inserting then -- If insert operation info: = 'insert'; elsif updating then -- if the modification operation info: = 'update'; else -- if the deletion operation info: = 'delete'; end if; insert into SQL _info values (Info); -- Record the operation information end my_trigger1; 4. delete trigger drop trigger my_trigger;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service