Notes -8: mysql trigger

1. Introduction Triggers

  • A trigger is a database object is assigned to a linked table, when a specific event occurs on a table, it will be activated.
  • mysqk trigger only supports: INSERT, UPDATE, DELETE statements.
  • Each table each event allows only a trigger, so each table supports up to six triggers.

2. Create Trigger

the Create  
            TRIGGER trigger_name trigger_time trigger_event
             ON tb_name the FOR EACH ROW trigger_body 

# trigger_name: trigger name. 
# Trigger_time: when the trigger is triggered. That the use of DEFORE, AFTER trigger is used to indicate a trigger before or after the statement that activated it. If you want to verify that the new data to meet the restrictions on the use, use BEFORE; or if you want to complete a few more changes after activation of the trigger statement is executed by using AFTER. 
# Trigger_event: the triggering event for the kind of statement specifies that activates the trigger. ( INSERT , UPDATE , DELETE ). 
# Tb_name: table name associated with the trigger. 
# The FOR EACH ROW: designated for each row affected by the triggering event trigger actions to be activated. 
# Trigger_body: trigger action body contains mysql statement trigger when activated will be executed.
# Create a trigger tb_student_insert_trigger in the table tb_student, for every time you insert a row of data into a table tb_student in the value of the variable str students to " ON Student added!"
 The Create  the Trigger tb_student_insert_trigger the After INSERT  ON tb_student the FOR EACH ROW the SET @ str = ' ON Student added! ' ; 

INSERT  INTO tb_student values ( ' 2013110101 ' , ' Zhang Xiaoyong ' , ' M ' , ' 1997-12-11 ' , ' Shanxi ' ,' Chinese ' , ' AC1301 ' ); 

SELECT @ STR ; # verification trigger

3. Check the trigger

SHOW TRIGGERS [{FROM | IN} db_name]

4. Delete Trigger

DROP TRIGGER [IF EXISTS] trigger_name
Delete tb_student trigger tb_student_insert_trigger table
 drop  the Trigger  IF  EXISTS tb_student_trigger;
  • When you delete a table, the table will automatically delete trigger associated.
  • Triggers can not be updated with the cover, if you need to modify the trigger, you must first remove and then re-created.

5. Trigger

 5.1 INSERT trigger

insert triggers may be performed before or after the insert statement execution. Need some attention:

  • You can refer to a virtual table at the NEW code to access the trigger insert rows are inserted.
  • In the before insert trigger, NEW values ​​can also be updated, i.e., allowed to change the value to be inserted.
  • In AUTO_INCREMENT column, NEW insert before execution is 0, the execution after the insert.
# In the table tb_student recreate the trigger tb_student_insert_trigger, for every time you insert a row into a table tb_student in the value of the variable str students to learn new insert number of students.
Create  Trigger tb_student_insert_trigger an AFTER the INSERT  ON tb_student the FOR EACH the ROW the SET @ STR = NEW.studentNo;

5.2 DELETE trigger

DELETE DELETE triggers can be performed before or after the statement is executed. Need some attention:

  • In the DELETE trigger code can reference a virtual table named OLD to access the deleted rows.
  • OLD value in all is read, it can not be updated.

5.3 UPDATE trigger

 UPDATE UPDATE statement trigger is executed before or after execution. Need some attention:

  • In the UPDATE trigger code can reference a table named OLD virtual access to the previous value, can also refer to a virtual table called NEW accessing the value of new updates.
  • In the BEFORE UPDATE trigger, the NEW value might also be updated, allowing to change the value of the UPDATE statement to be used.
  • OLD value in all is read-only, can not be updated.
  • When the trigger operation involves updating the table itself, use only BEFORE UPDATE trigger, AFTER UPDATE triggers can not be used.
# Create a table tb_student tb_student_update_trigger the trigger, the value for each update table a value column tb_student nation native column.
Create  Trigger tb_student_update_trigger the BEFORE the UPDATE  the ON tb_student the FOR EACH the ROW the SET NEW.nation = OLD.native; 

Update tb_student the SET Nation = ' strong '  WHERE studentName = ' Zhang Xiaoyong ' ;

 

Guess you like

Origin www.cnblogs.com/Cyzhouke/p/11490407.html