Trigger (create, view, use, delete)

First, create a trigger

Trigger (TRIGGER) by INSERT , UPDATE , and DELETE and other events to trigger a particular operation. Trigger trigger condition is satisfied, the database system will execute a program statement trigger defined. This ensures consistency between certain operations.

1 , create a trigger only one statement is executed, the basic form as follows:

CREATE TRIGGER   trigger name   BEFORE | AFTER   trigger event

ON    table   FOR EACH ROW execute the statement

Among them, the trigger name  the name parameter refers to the creation of the trigger; the BEFORE and AFTER parameter specifies the trigger execution times, the BEFORE refers to execute the triggering statement before the trigger event, AFTER refers to the implementation of the triggering statement after the trigger event; trigger event parameter refers to trigger conditions, including INSERT , UPDATE , and DELETE ; table name argument refers to the name of the table the trigger event operations; the FOR EACH ROW indicates the operation to meet on a record of any trigger events will trigger the trigger; the trigger is executing a statement parameter refers to program to be executed after the trigger.

mysql> CREATE TRIGGER dept_trig1 BEFORE INSERT

    -> ON department FOR EACH ROW

    -> INSERT INTO triger_time VALUES(NOW());

Description: When the department execution table INSERT operation, the database system will be in the INSERT the statement is executed before triger_time into the current time table.

2 , create multiple triggers execution of the statement, the basic form as follows:

CREATE TRIGGER   trigger name   BEFORE | AFTER   trigger event

ON  表名  FOR  EACH  ROW

BEGIN

Execute the statement list

END

Wherein, the BEGIN and END intermediate statement list is executed parameter indicates the need to perform a plurality of execution contents of the statement. Separated by a semicolon between different execution statements.

Normally MySQL default is ";" ( semicolon ) as the end of the statement is executed. In the process, create a trigger need to use a semicolon, in order to solve this problem, you can use DELIMITER statement. as follows:

mysql> DELIMITER &&

mysql> CREATE TRIGGER dept_trig2 AFTER DELETE

    -> ON department FOR EACH ROW

    -> BEGIN

    -> INSERT INTO trigger_time VALUES('21:01:01');                                            

    -> INSERT INTO trigger_time VALUES('22:01:01');                                            

    -> END

    -> &&

 

Second, view the trigger

View trigger is a defined view information already in the database trigger, status and grammar. View trigger methods include SHOW TRIGGERS statements and queries information_schma in the database triggers lists.

mysql> SHOW TRIGGERS \ G query information of all the triggers can not query the specified trigger

mysql> SELECT * FROM information_schema.triggers \ G query tiggers information will display detailed information about all triggers

mysql> SELECT * FROM information_schema.triggers WHERE TRIGGER_NAME = 'dept_trig1' \ G query only dept_trig1 details trigger.

 

Three , using the trigger

In MySQL , the order of execution of the trigger is BEFORE trigger table operation (the INSERT , the UPDATE , and DELETE) and AFTER triggers.

In the department created on the table BEFORE INSERT and AFTER INSERT two triggers.

mysql> CREATE TRIGGER before_insert BEFORE INSERT

    -> ON department FOR EACH ROW

    -> INSERT INTO trigger_test VALUES (null , "before_insert"); create a BEFORE INSERT trigger

mysql> CREATE TRIGGER after_insert AFTER INSERT

    -> ON department FOR EACH ROW

    -> INSERT INTO trigger_test VALUES (null , "after_insert"); create AFTER INSERT trigger

mysql> INSERT INTO department VALUES (1003 , 'HAOHOA', 'HELLO', 'YES'); the department inserts a record for the previous experiment two trigger operation sequence.

mysql> SELECT * FROM trigger_test; you can view BEFORE INSERT prior to the AFTER INSERT operation

+----+---------------+

| id | info          |

+----+---------------+

| 1 | before_insert |

| 2 |   after_insert  |

+----+---------------+

1 row in set (0.00 sec)

 

Four , remove the trigger

Delete Delete trigger finger already in the database trigger. The basic form is as follows:

DROP TRIGGER   trigger name | database name . Trigger name ;

Among them, the trigger name  the name parameter refers to the deletion of the trigger. If you specify only the name of the trigger, the database system will look for the trigger in the current database, if found to be deleted. Trigger name | database name . Trigger name refers to both choose one of them. We no longer need a trigger must be removed.

mysql> DROP TRIGGER test2.dept_trig1; specify delete the database test2 trigger under dept_trig1 , if you do not specify a database test2, then delete the current trigger in the database dept_trig1 .

 

Guess you like

Origin www.cnblogs.com/uphold/p/11222004.html