触发器(trigger):监视某种情况,并触发某种操作。

触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/before) 4.触发事件

语法:

CREATE TRIGGER trigger_name trigger_time trigger_event
    ON tbl_name FOR EACH ROW trigger_stmt

触发程序与命名为tbl_name的表相关。tbl_name必须引用永久性表。不能将触发程序与临时表表或视图关联起来。

trigger_time是触发程序的动作时间。它可以是before或after,以指明触发程序是在激活它的语句之前或之后触发。

trigger_event指明了激活触发程序的语句的类型。trigger_event可以是下述值之一:

   insert :将新行插入表时激活触发程序,例如,通过insert、load data和replace语句。

  update:更改某一行时激活触发程序,例如,通过update语句。

     delete :从表中删除某一行时激活触发程序,例如,通过delete和replace语句。

要注意,trigger_event与以表操作方式激活触发程序的SQL语句并不很类似,这点很重要。

例如:关于insert的before触发程序不仅能被insert语句激活,也能被load data语句激活。

create trigger triggerName

after/before insert/update/delete on 表名

for each row   #这句话在mysql是固定的

begin

sql语句;

end;


对于insert语句, 只有new是合法的;

对于delete语句,只有old才合法;

对于update语句,new、old可以同时使用。


创建表(触发器要操作的两张表)

/*auto_increment:自增;priamry key :主键;comment:注释*/

/* drop:删除;if exists xxx(判断xxx名在数据库时候是否出存在xxx名称)*/

/* for each row :循环一行一行的执行数据 */

/* after insert/update/delete on table_name :针对哪个表执行的insert/update/delete 操作 */

drop table if exists table1;

create table table1(
id ) primary key auto_increment not null comment 'id',
name ) comment '名字'
);

drop table if exists table2;
create table table2(
id int primary key auto_increment not null comment 'id',
name ) comment '名字'
);

Before与After区别:

before:(insert、update)可以对new进行修改,after不能对new进行修改,两者都不能修改old数据。

insert 触发器

drop trigger if exists insert_on_table1;
create trigger insert_on_table1
after insert  on table1
for each row
begin
insert into table2(name) value(new.name);
end

操作触发器

insert table1(name) value('aaa');

查询table2是否有值

select * from table2;

delete触发器

drop trigger if exists delete_on_table1;
create trigger delete_on_table1
after delete on table1
for each ROW
begin
delete from table2 where name=old.name;
end

执行删除操作

;

查询table2变化

select * from table2;

更新table1更新触发器

drop trigger if exists update_on_table1;
create trigger update_on_table1
after update on table1
for each ROW
begin
update table2 set name=new.name where name=old.name;
end

执行更新操作

update table1 set name='ccc';

查询table2变化

select * from table2;

使用before 统计插入积分例子:

创建表

drop table if exists table3;
create table table3(
id int primary key auto_increment comment 'id',
num int  comment '积分'
)engine=myisam  default charset=utf8 comment='单独积分表';

创建用函数变量接收的触发器

drop trigger if exists insert_on_table3;
create trigger insert_on_table3
before insert on table3
for each row
set @sum=@sum+new.num;

执行触发器

;
,),(,),(,),(,);
select @sum;

mysql 触发器(trigger)的更多相关文章

  1. mysql触发器trigger 实例详解

    mysql触发器trigger 实例详解 (转自 https://www.cnblogs.com/phpper/p/7587031.html)   MySQL好像从5.0.2版本就开始支持触发器的功能 ...

  2. 如何使用MySQL触发器trigger

    阅读目录:触发器trigger的使用 创建触发器 单一执行语句.多执行语句 new.old详解 查看触发器 删除触发器:慎用触发器,不用就删除 Q:什么是触发器? A: 触发器是与表有关的数据库对象, ...

  3. mysql 触发器 trigger用法 two (稍微复杂的)

    触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/befo ...

  4. MySQL触发器Trigger实例篇

    定义: 何为MySQL触发器? 在MySQL Server里面也就是对某一个表的一定的操作,触发某种条件(Insert,Update,Delete 等),从而自动执行的一段程序.从这种意义上讲触发器是 ...

  5. MySQL触发器 trigger学习

    触发器:一类特殊的事物.可监视某种数据操作,并触发相关操作(insert/update/delete).表中的某些数据改变,希望同一时候能够引起其他相关数据改变的需求. 作用:变化自己主动完毕某些语句 ...

  6. MySQL 触发器trigger

    一.触发器概念 触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(af ...

  7. MySQL触发器 trigger之for each row

    for each row 每行受影响,触发器都运行.叫行级触发器. oracle 触发器中分行级触发器和语句级触发器,可不写for each row,不管影响多少行都仅仅运行一次. mysql不支持语 ...

  8. (转)MySQL触发器trigger示例详解

    一.什么是触发器 触发器是与表有关的数据库对象,在满足定义条件时触发,并执行触发器中定义的语句集合.触发器的这种特性可以协助应用在数据库端确保数据的完整性. 举个例子,比如你现在有两个表[用户表]和[ ...

  9. mysql 触发器 trigger用法 three (稍微复杂的)

    MySQL包含对触发器的支持.触发器是一种与表操作有关的数据库对象,当触发器所在表上出现指定事件时,将调用该对象,即表的操作事件触发表上的触发器的执行. 创建触发器 在MySQL中,创建触发器语法如下 ...

随机推荐

  1. [leetcode-554-Brick Wall]

    There is a brick wall in front of you. The wall is rectangular and has several rows of bricks.The br ...

  2. 【LeetCode】73. Set Matrix Zeroes

    题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Fo ...

  3. 屏幕适配/autoLayout autoresizingMask

    #pragma mark-- 屏幕适配/autoLayout autoresizingMask 1> 发展历程 代码计算frame -> autoreszing(父控件和子控件的关系) - ...

  4. 普通RAID磁盘数据格式规范

    普通RAID磁盘数据格式规范 1.介绍 在当今的IT环境中,系统管理员希望改变他们正在使用的内部RAID方案,原因可能有以下几个:许多服务器都是附带RAID解决方案的,这些RAID解决方案是通过母板磁 ...

  5. C#调用TSC条码打印机打印二维码

    #region 调用TSC打印机打印 /// <summary> /// 调用TSC打印机打印 /// </summary> /// <param name=" ...

  6. Failed to sync Gradle project 'XX'错误解决

    错误代码 Failed to sync Gradle project 'WeChat' Error:Failed to find target with hash string 'android-24 ...

  7. 【总算解决了】A network-related or instance-specific error occurred while establishing a connection to SQL Server

    给别人做的网站莫名其妙连接不上数据库.百度了好多,总算知道自己的错在哪了. 报 "A network-related or instance-specific error occurred  ...

  8. python中的可变与不可变对象

    Python中的可变对象和不可变对象 什么是可变/不可变对象 不可变对象,该对象所指向的内存中的值不能被改变.当改变某个变量时候,由于其所指的值不能被改变,相当于把原来的值复制一份后再改变,这会开辟一 ...

  9. asp.net core高级应用:TagHelper+Form

    上一篇博客我讲解了TagHelper的基本用法和自定义标签的生成,那么我就趁热打铁,和大家分享一下TagHelper的高级用法~~,大家也可以在我的博客下随意留言. 对于初步接触asp.net cor ...

  10. C#字典转换成where条件

    where 1=1 and Dictionary[key1]=Dictionary[value1] and Dictionary[key2]=Dictionary[value3].... /// &l ...