mysql trigger trigger Short description

Brief introduction

Mysql trigger belongs to an advanced feature, which can be regarded as a simplified version of the stored procedure. And real stored procedure different is that it is not being called call, but executed automatically after detecting the operating condition is satisfied, similar to the relationship between system monitoring and alarms.

mysql trigger only supports trigger line, is always directed to a record. mysql trigger is triggered for the insert to the row \ update \ delete operations, specifically to determine which action is triggered by actual defined by the user. But each event for each table, can only define a trigger.

mysql trigger trigger time is divided into two: before and after, that is, before or after the trigger event execution. For example, says that before insert trigger before the insert operation.

Trigger relatively advantageous, it is possible to reduce the communication between the client and the server, which can improve performance; it can cover the specific server behind the work front, can enhance the confidentiality of data; it can also ensure consistent data of course, limited to the operation innodb table, the trigger innodb table is done in the same transaction.

There are advantages and disadvantages will be the same, because hide a lot of concrete work, the trigger will be more difficult to troubleshoot problems; unclear if the server has a trigger operation, it is almost impossible to locate the problem.

Considering the pros and cons of using a trigger system, and then decide whether to use triggers in their own system.

Syntax Rules

create trigger [trigger_name] [trigger_time] [trigger_event] on [table_name] for each row [trigger_operation]

name of the trigger being created;: trigger_name
trigger_time: the execution time of the trigger;
trigger_event: Trigger dependent triggering event;
table_name: the role of the trigger table;
trigger_operation: specific trigger action statements;

Examples

Create two tables, a t_trigger_user to save personnel information, salary information is used to save a t_trigger_salary personnel. Two empty table to test the specific table structure as follows:

mysql> show create table t_trigger_user\G
*************************** 1. row ***************************
       Table: t_trigger_user
Create Table: CREATE TABLE `t_trigger_user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(32) DEFAULT '',
  `age` int(3) unsigned DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> show create table t_trigger_salary\G
*************************** 1. row ***************************
       Table: t_trigger_salary
Create Table: CREATE TABLE `t_trigger_salary` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `salary` int(10) unsigned DEFAULT '0',
  `uid` int(10) NOT NULL,
  `name` varchar(32) DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

Insert test operation of the trigger

First, create a trigger-based insert operation t_trigger_user table (by default all of our operations are triggered after a specific operation), the trigger used to complete "when new data t_trigger_user table insert, insert a row in sync t_trigger_salary table Related Information".
Create a trigger after insert

mysql> create trigger user_insert after insert on t_trigger_user for each row insert into t_trigger_salary(uid,name) values(new.id,new.name);

Perform operations to insert the table where the trigger t_trigger_user, and then view the information in both tables

mysql> insert into t_trigger_user(name,age) values('lily',21);                                                                           
Query OK, 1 row affected (0.01 sec)

mysql> select * from t_trigger_user;
+----+------+------+
| id | name | age  |
+----+------+------+
|  2 | lily |   21 |
+----+------+------+
1 row in set (0.00 sec)

mysql> select * from t_trigger_salary;
+----+--------+-----+------+
| id | salary | uid | name |
+----+--------+-----+------+
|  1 |      0 |   2 | lily |
+----+--------+-----+------+
1 row in set (0.00 sec)

You can see, t_trigger_user table after creating a trigger insert operation, after performing the insert operation, data insertion is successful, also fired the trigger execution to t_trigger_salary also insert a table of data.

By the same token, test again trigger update and delete operations to achieve operating.

update exemplary operation of the trigger

mysql> delimiter //
mysql> create trigger update_user after update on t_trigger_user for each row
    -> begin
    -> if new.name != old.name
    -> then
    -> update t_trigger_salary set name=new.name where uid=old.id;
    -> end if;
    -> end
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> update t_trigger_user set name='anvil' where id=2;
    -> //
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> delimiter ;
mysql> select * from t_trigger_user;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  2 | anvil |   21 |
+----+-------+------+
1 row in set (0.00 sec)

mysql> select * from t_trigger_salary;
+----+--------+-----+-------+
| id | salary | uid | name  |
+----+--------+-----+-------+
|  1 |      0 |   2 | anvil |
+----+--------+-----+-------+
1 row in set (0.00 sec)

delete exemplary operation of the trigger

mysql> create trigger delete_user after delete on t_trigger_user for each row delete from t_trigger_salary where uid=old.id;
Query OK, 0 rows affected (0.01 sec)

mysql> delete from t_trigger_user where id=2;
Query OK, 1 row affected (0.00 sec)

mysql> select * from t_trigger_user;
Empty set (0.00 sec)

mysql> select * from t_trigger_salary;
Empty set (0.00 sec)

Yes, it triggers [trigger_operation] not only supports a single sql statement of operations, statement is also supported by a series of operations begin ... .end pack up.

When the trigger is triggered, then the error occurred when the operation of the trigger will not be executed, the same, triggering the trigger action statements will not be actually carried out.

Published 105 original articles · won praise 58 · views 410 000 +

Guess you like

Origin blog.csdn.net/ljl890705/article/details/78329866