MySQL 触发器示例
简介:
MySQL 触发器
这次实验是在一台 MySQL Slave 上进行的,事实证明:从库添加数据库、表、插入、删除数据等,不会导致主从失败。
一、创建实验数据库、表
mysql > create database trdb default character set utf8; mysql > create table trdb.t_film (id int(5) primary key auto_increment, name varchar(32), cid int(3), status int(1)); mysql > desc trdb.t_film;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(5) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
| cid | int(3) | YES | | NULL | |
| status | int(1) | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+ mysql > create table trdb.t_tr (id int(5) primary key auto_increment, vid int(5), opertion int(1)); mysql > desc trdb.t_tr;
+----------+--------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------+------+-----+---------+----------------+
| id | int(5) | NO | PRI | NULL | auto_increment |
| vid | int(5) | YES | | NULL | |
| opertion | int(1) | YES | | NULL | |
+----------+--------+------+-----+---------+----------------+
# 表一:t_film 为业务数据表,执行 INSERT、UPDATE、DELETE 操作
# 表二:t_tr 为触发器程序体数据写入表
二、创建触发器
mysql > use trdb; mysql > delimiter $ # 定义界定符,默认 ;
1、INSERT 事件
mysql > create trigger tr_insert after insert on t_film for each row
-> begin
-> if new.cid in (2, 3, 4, 5, 6, 45) then
-> insert into t_tr set vid = new.id, opertion = 1;
-> end if;
-> end$ # 语法介绍:
# 创建一个触发器,名为 tr_insert
# 触发时机,事件发生前 before、事件发生后 after
# 事件类型,insert、update、delete
# 为哪张表创建触发器,t_film ( 同一张表不能同时创建相同类型的触发器 )
# 触发器执行间隔,row 每行触发一次
# begin ... end,区域内可以写逻辑、多条SQL语句 begin
declare uid int(11);
set uid = (select uid from table where uid = new.uid);
if new.uid = uid then
insert into trdb.tr_t set vid = new.fid, opertion = 1;
end if;
end$ # 变量赋值
# NEW、OLD
# INSERT 触发器时,NEW 表示 BEFORE 将要或 AFTER 已经插入的新数据
# UPDATE 触发器时,OLD 表示 BEFORE 将要或 AFTER 已经被修改的原数据,NEW 表示 BEFORE 将要或 AFTER 已经被修改为的新数据
# DELETE 触发器时,OLD 表示 BEFORE 将要或 AFTER 已经被删除的原数据
2、UPDATE 事件
mysql > create trigger tr_update after update on t_film for each row
-> begin
-> if new.cid in (2, 3, 4, 5, 6, 45) then
-> insert into t_tr set vid = new.id, opertion = 2;
-> end if;
-> end$
3、DELETE 事件
mysql > create trigger tr_delete after delete on t_film for each row
-> begin
-> if old.cid in (2, 3, 4, 5, 6, 45) then
-> insert into t_tr set vid = old.id, opertion = 3;
-> end if;
-> end$ mysql > delimiter ;
4、查看触发器
mysql > show triggers\G
*************************** 1. row ***************************
Trigger: tr_insert
Event: INSERT
Table: t_film
Statement: begin
if new.cid in (2, 3, 4, 5, 6, 45) then
insert into t_tr set vid = new.id, opertion = 1;
end if;
end
Timing: AFTER
Created: NULL
sql_mode:
Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
*************************** 2. row ***************************
Trigger: tr_update
Event: UPDATE
Table: t_film
Statement: begin
if new.cid in (2, 3, 4, 5, 6, 45) then
insert into t_tr set vid = new.id, opertion = 2;
end if;
end
Timing: AFTER
Created: NULL
sql_mode:
Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
*************************** 3. row ***************************
Trigger: tr_delete
Event: DELETE
Table: t_film
Statement: begin
if old.cid in (2, 3, 4, 5, 6, 45) then
insert into t_tr set vid = old.id, opertion = 3;
end if;
end
Timing: AFTER
Created: NULL
sql_mode:
Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
三、验证触发器
mysql > select * from t_film;
Empty set (0.00 sec) mysql > select * from t_tr;
Empty set (0.00 sec)
1、INSERT 事件验证
mysql > insert into t_film set name = '警察故事 1', cid = 2, status = 1; mysql > select * from t_film;
+----+----------------+------+--------+
| id | name | cid | status |
+----+----------------+------+--------+
| 1 | 警察故事 1 | 2 | 1 |
+----+----------------+------+--------+ mysql > select * from t_tr;
+----+------+----------+
| id | vid | opertion |
+----+------+----------+
| 1 | 1 | 1 |
+----+------+----------+
# 触发器达到效果,数据被写入
mysql > insert into t_film set name = '警察故事 2', cid = 41, status = 0; mysql > select * from t_film;
+----+----------------+------+--------+
| id | name | cid | status |
+----+----------------+------+--------+
| 1 | 警察故事 1 | 2 | 1 |
| 2 | 警察故事 2 | 41 | 0 |
+----+----------------+------+--------+ mysql > select * from t_tr;
+----+------+----------+
| id | vid | opertion |
+----+------+----------+
| 1 | 1 | 1 |
+----+------+----------+
# 触发器达到效果,数据没有被写入,因为 cid not in (2, 3, 4, 5, 6, 45)
2、UPDATE 事件验证
mysql > update t_film set cid = 2, status = 1 where name = '警察故事 2'; mysql > select * from t_tr;
+----+------+----------+
| id | vid | opertion |
+----+------+----------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
+----+------+----------+
# 触发器达到效果,数据被写入
3、DELETE 事件验证
mysql > insert into t_film set name = '警察故事 3', cid = 41, status = 0; mysql > select * from t_film;
+----+----------------+------+--------+
| id | name | cid | status |
+----+----------------+------+--------+
| 1 | 警察故事 1 | 2 | 1 |
| 2 | 警察故事 2 | 2 | 1 |
| 3 | 警察故事 3 | 41 | 0 |
+----+----------------+------+--------+ mysql > delete from t_film where name = '警察故事 1'; mysql > delete from t_film where name = '警察故事 3'; mysql > select * from t_tr;
+----+------+----------+
| id | vid | opertion |
+----+------+----------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 1 | 3 |
+----+------+----------+
# 触发器达到效果,只有删除 警察故事 1 的事件被记录
4、批量操作
mysql > select * from t_film;
+----+----------------+------+--------+
| id | name | cid | status |
+----+----------------+------+--------+
| 2 | 警察故事 2 | 2 | 1 |
| 4 | 警察故事 3 | 41 | 0 |
| 5 | 警察故事 4 | 41 | 0 |
| 6 | 警察故事 5 | 41 | 0 |
| 7 | 警察故事 6 | 41 | 0 |
| 8 | 警察故事 7 | 41 | 0 |
| 9 | 警察故事 8 | 41 | 0 |
| 10 | 警察故事 9 | 41 | 0 |
+----+----------------+------+--------+ mysql > select * from t_tr;
+----+------+----------+
| id | vid | opertion |
+----+------+----------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 1 | 3 |
+----+------+----------+ mysql > update t_film set cid = 2, status = 1 where cid = 41; mysql > select * from t_tr;
+----+------+----------+
| id | vid | opertion |
+----+------+----------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 1 | 3 |
| 4 | 4 | 2 |
| 5 | 5 | 2 |
| 6 | 6 | 2 |
| 7 | 7 | 2 |
| 8 | 8 | 2 |
| 9 | 9 | 2 |
| 10 | 10 | 2 |
+----+------+----------+ mysql > delete from t_film; mysql > select * from t_tr where id > 10;
+----+------+----------+
| id | vid | opertion |
+----+------+----------+
| 11 | 2 | 3 |
| 12 | 4 | 3 |
| 13 | 5 | 3 |
| 14 | 6 | 3 |
| 15 | 7 | 3 |
| 16 | 8 | 3 |
| 17 | 9 | 3 |
| 18 | 10 | 3 |
+----+------+----------+
# 批量操作也没有问题!
MySQL 触发器示例的更多相关文章
- mysql——触发器——示例
数据准备: ), d_id ), name ), age ), sex ), homeadd ) ); ,,,'nan','beijing'); ,,,'nv','hunan'); ,,,'nan', ...
- mysql 触发器示例和注解
-- 格式 CREATE TRIGGER 触发器名称 AFTER|before insert|update|delete ON 触发表 FOR EACH ROW BEGIN insert into 处 ...
- Mysql触发器示例
begin ); ); ); then set x = (select ID from qn_huiyuan_grade g ); elseif r then set x = (select ID f ...
- 【mysql】mysql触发器使用示例
mysql触发器 时间点:before/after 触发事件: update/delete/insert 时间点+触发事件:构成一个完整的触发器的触发时机: 一个触发时机最多只能由1个Trigger: ...
- MySQL触发器使用详解
MySQL包含对触发器的支持.触发器是一种与表操作有关的数据库对象,当触发器所在表上出现指定事件时,将调用该对象,即表的操作事件触发表上的触发器的执行. 创建触发器在MySQL中,创建触发器语法如下: ...
- MySQL 触发器结构及三个案例demo
--你必须拥有相当大的权限才能创建触发器(CREATE TRIGGER),如果你已经是Root用户,那么就足够了.这跟SQL的标准有所不同. CREATE TRIGGER语法 CREATE TRIGG ...
- mysql 触发器 trigger用法 three (稍微复杂的)
MySQL包含对触发器的支持.触发器是一种与表操作有关的数据库对象,当触发器所在表上出现指定事件时,将调用该对象,即表的操作事件触发表上的触发器的执行. 创建触发器 在MySQL中,创建触发器语法如下 ...
- Day4 MySql触发器视图索引以及设计优化
触发器 MySQL包含对触发器的支持.触发器是一种与表操作有关的数据库对象,当触发器所在表上出现指定事件时,将调用该对象,即表的操作事件触发表上的触发器的执行. 通过事件触发,不能传参 语法 CREA ...
- 数据库-mysql触发器
MySQL包含对触发器的支持.触发器是一种与表操作有关的数据库对象,当触发器所在表上出现指定事件时,将调用该对象,即表的操作事件触发表上的触发器的执行. 一:创建触发器 在MySQL中,创建触发器语法 ...
随机推荐
- JavaScript运算符:递增和递减(++i,--i 和 i++,i-- 的区别)
递增和递减操作符直接借鉴自C,而且各有两个版本:前置型 (递增 ++i ,递减 --i )和 后置型 (递增 i++ ,递减 i-- ).书本上对两者的定义是:前置型应该位于要操作的变量之前,而后置型 ...
- 安装MySQL后要做的事
安装MySQL后要修改的配置 [mysql] default-character-set=utf8 [mysqld] # 关闭域名反解 skip_name_resolve # 每表一个独立的表空间文件 ...
- 在Jsp中调用静态资源,路径配置问题,jsp获取路径的一些方法
在Jsp中调用图片.JS脚本等,针对取得的路径有两种调用方式: 1.放入Body中生成绝对路径(不建议) <%@ page language="java" import=&q ...
- istringstream、ostringstream、stringstream类介绍
本文转载自: http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html 一.C++的输入输出分为三种: (1)基于控制台的I/O ( ...
- react 拖拽排序---原生
定义css, 两个动画 .drag-up { -webkit-animation: dragup ease 0.2s 1; animation: dragup ease 0.2s 1; -webkit ...
- github打开慢,页面打不开,请求老是失败问题修复总结
感谢老铁 QQ(1218624820) 提供的方法建议 原因来自于DNS污染, 到下面的目录进行修改文件 C:\Windows\System32\drivers\etc 在后面粘贴下面的信息 192. ...
- ionic2中跨页面回传值
1.在跳转到新页面时传入一个contactsCallback的参数,在该参数的函数定义中做出一个承诺. 注意:最开始我本来是采用如下图方式的,但是很不幸,出现了问题,问题所在就是关于这个this的作用 ...
- create newline in Github Bio
/********************************************************************************* * create newline ...
- CentOS6.6安装(转)
2015-3-6CentOS6.6安装 环境:工控机 1.选择安装software development workstation,其他参考以下文档过程 一.安装CentOS 6.6,安装结束重新启动 ...
- Dubbo 基础教程
原文地址:Dubbo 基础教程 博客地址:http://www.extlight.com 一.前言 当服务越来越多时,容量的评估,小服务资源的浪费等问题逐渐显现,此时需要增加一个调度中心基于访问压力实 ...