创建触发器:
CREATE
[DEFINER = {user|CURRENT_USER}]
TRIGGER trigger_name
trigger_time trigger_event
ON tbl_name FOR EACH ROW
trigger_body
语法中:
trigger_name:触发器的名称,不能与已经存在的触发器重复; trigger_time:{ BEFORE | AFTER },表示在事件之前或之后触发; trigger_event::{ INSERT |UPDATE | DELETE },触发该触发器的具体事件; tbl_name:该触发器作用在tbl_name上;
实例:
创建简单确发器
<1> 准备学生表和学生数目统计表
mysql> CREATE TABLE student_info (
-> stu_no INT(11) NOT NULL AUTO_INCREMENT,
-> stu_name VARCHAR(255) DEFAULT NULL,
-> PRIMARY KEY (stu_no)
-> );
mysql> CREATE TABLE student_count(
-> student_count INT(11) DEFAULT 0
-> );
插入一条数据:
mysql> INSERT INTO student_count VALUES(0);
<2> 创建简单触发器,在向学生表INSERT数据时,学生数增加,DELETE学生时,学生数减少
http://blog.csdn.net/goskalrie/article/details/53020631
mysql> CREATE TRIGGER trigger_student_count_insert
-> AFTER INSERT
-> ON student_count FOR EACH ROW
-> UPDATE student_count SET student_count=student_count+1;
mysql> CREATE TRIGGER trigger_student_count_insert
-> AFTER INSERT
-> ON student_info FOR EACH ROW
-> UPDATE student_count SET student_count=student_count+1; mysql> CREATE TRIGGER trigger_student_count_delete
-> AFTER DELETE
-> ON student_info FOR EACH ROW
-> UPDATE student_count SET student_count=student_count-1;
<3> INSERT、DELETE数据,查看触发器是否正常工作
mysql> INSERT INTO student_info VALUES(NULL,'xiaoc'),(NULL,'xiaoz'),(NULL,'xionan');
mysql> select * from student_info;
+--------+----------+
| stu_no | stu_name |
+--------+----------+
| 1 | xiaoc |
| 2 | xiaoz |
| 3 | xionan |
+--------+----------+
3 rows in set (0.00 sec) mysql> desc student_info;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| stu_no | int(11) | NO | PRI | NULL | auto_increment |
| stu_name | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
删除确发器,修改确发器:
触发器会随着表的删除被删除!
查看触发器:
show triggers; 正解版:
mysql> CREATE TABLE student_info (
->
-> stu_no INT(11) NOT NULL AUTO_INCREMENT,
->
-> stu_name VARCHAR(255) DEFAULT NULL,
->
-> PRIMARY KEY (stu_no)
->
-> );
Query OK, 0 rows affected (0.22 sec) mysql> CREATE TABLE student_count (
->
-> student_count INT(11) DEFAULT 0
->
-> );
Query OK, 0 rows affected (0.24 sec) mysql> INSERT INTO student_count VALUES(0);
Query OK, 1 row affected (0.14 sec) mysql> CREATE TRIGGER trigger_student_count_insert
->
-> AFTER INSERT
->
-> ON student_info FOR EACH ROW
->
-> UPDATE student_count SET student_count=student_count+1;
Query OK, 0 rows affected (0.09 sec) mysql> CREATE TRIGGER trigger_student_count_delete
->
-> AFTER DELETE
->
-> ON student_info FOR EACH ROW
->
-> UPDATE student_count SET student_count=student_count-1;
Query OK, 0 rows affected (0.14 sec) mysql> INSERT INTO student_info VALUES(NULL,'xiaoc'),(NULL,'xiaoz'),(NULL,'xionan');
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from student_info;
+--------+----------+
| stu_no | stu_name |
+--------+----------+
| 1 | xiaoc |
| 2 | xiaoz |
| 3 | xionan |
+--------+----------+
3 rows in set (0.00 sec) mysql> select * from student_count;
+---------------+
| student_count |
+---------------+
| 3 |
+---------------+
1 row in set (0.00 sec)
mysql> delete from student_info where stu_name in ('xionan','xiaoc');
Query OK, 2 rows affected (0.06 sec) mysql> select * from student_count;
+---------------+
| student_count |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
mysql> insert into student_info values(null,'xiaol');
Query OK, 1 row affected (0.06 sec) mysql> select * from student_info;
+--------+----------+
| stu_no | stu_name |
+--------+----------+
| 2 | xiaoz |
| 4 | xiaol |
+--------+----------+
2 rows in set (0.00 sec) mysql> select * from student_count;
+---------------+
| student_count |
+---------------+
| 2 |
+---------------+
1 row in set (0.00 sec) 可以看到无论是INSERT还是DELETE学生,学生数目都会跟变化的。
创建包含多条执行语句的触发器:
在trigger_body中可以执行多条SQL语句,此时的trigger_body需要使用BEGIN和END做为开始和结束的标志:
CREATE [DEFINER = { user | CURRENT_USER }] TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW BEGIN trigger_statement END; 示例2,创建包含多条执行语句的触发器:
(删除触发器)
mysql> show triggers\G;
*************************** 1. row ***************************
Trigger: trigger_student_count_insert
Event: INSERT
Table: student_info
Statement: UPDATE student_count SET student_count=student_count+1
Timing: AFTER
Created: NULL
sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
Definer: root@127.0.0.1
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
*************************** 2. row ***************************
Trigger: trigger_student_count_delete
Event: DELETE
Table: student_info
Statement: UPDATE student_count SET student_count=student_count-1
Timing: AFTER
Created: NULL
sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
Definer: root@127.0.0.1
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
2 rows in set (0.00 sec) ERROR:
No query specified mysql> DROP TRIGGER trigger_student_count_insert;
Query OK, 0 rows affected (0.10 sec) mysql> DROP TRIGGER trigger_student_count_delete;
Query OK, 0 rows affected (0.00 sec)
mysql> show triggers;
Empty set (0.00 sec)
依然沿用上面的例子中的表,对student_count表做如下变更:增加student_class字段表示具体年级的学生数,其中0表示全年级,1代表1年级……;同样学生表中也增加该字段。清空两个表中的所有数据。
1:对student_count表增加student_class字段:
mysql> show create table student_count;
+---------------+-------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------+-------------------------------------------------------------------------------------------------------------+
| student_count | CREATE TABLE `student_count` (
`student_count` int(11) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------------+-------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> alter table student_count add student_class int(11) default '0';
Query OK, 0 rows affected (0.33 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table student_count;
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| student_count | CREATE TABLE `student_count` (
`student_count` int(11) DEFAULT '0',
`student_class` int(11) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
<1> 删除上例中的两个触发器,初始化student_count表中数据,插入三条数据(0,0),(1,0),(2,0)表示全年级、一年级、二年级的初始人数都是0;
mysql> update student_count set student_count=0;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from student_count;
+---------------+---------------+
| student_count | student_class |
+---------------+---------------+
| 0 | 0 |
+---------------+---------------+
1 row in set (0.00 sec) mysql> insert student_count values(1,0),(2,0);
Query OK, 2 rows affected (0.07 sec)
Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from student_count;
+---------------+---------------+
| student_count | student_class |
+---------------+---------------+
| 0 | 0 |
| 1 | 0 |
| 2 | 0 |
+---------------+---------------+
3 rows in set (0.00 sec) <2> 创建触发器,在INSERT时首先增加学生总人数,然后判断新增的学生是几年级的,再增加对应年级的学生总数:
mysql> select * from student_info;
+--------+----------+
| stu_no | stu_name |
+--------+----------+
| 2 | xiaoz |
| 4 | xiaol |
+--------+----------+
2 rows in set (0.00 sec) mysql> delete from student_info where stu_name='xiaoz';
Query OK, 1 row affected (0.03 sec) mysql> delete from student_info where stu_name='xiaol';
Query OK, 1 row affected (0.06 sec) mysql> select * from student_info;
Empty set (0.00 sec)
上面是先清理一下数据: 字段的增加有误,顺序有误:
mysql> alter table student_count drop student_count;
Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student_count;
+---------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+-------+
| student_class | int(11) | YES | | 0 | |
+---------------+---------+------+-----+---------+-------+
1 row in set (0.00 sec) mysql> alter table student_count add student_count int(11) default '0';
Query OK, 0 rows affected (0.41 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table student_count;
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| student_count | CREATE TABLE `student_count` (
`student_class` int(11) DEFAULT '0',
`student_count` int(11) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> delete from student_count where student_class=0;
Query OK, 3 rows affected (0.06 sec) 重新插入数据:
mysql> insert student_count values(0,0),(1,0),(2,0);
Query OK, 3 rows affected (0.08 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from student_count;
+---------------+---------------+
| student_class | student_count |
+---------------+---------------+
| 0 | 0 |
| 1 | 0 |
| 2 | 0 |
+---------------+---------------+
3 rows in set (0.00 sec)
http://blog.csdn.net/goskalrie/article/details/53020631
对student_count表做如下变更:增加student_class字段表示具体年级的学生数,其中0表示全年级,
1代表1年级……;同样学生表中也增加该字段。清空两个表中的所有数据。
mysql> alter table student_info add column student_class int//
Query OK, 0 rows affected (1.31 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student_info//
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| stu_no | int(11) | NO | PRI | NULL | auto_increment |
| stu_name | varchar(255) | YES | | NULL | |
| student_class | int(11) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec) 创建触发器,在INSERT时首先增加学生总人数,然后判断增加学生总人数,然后判断新增的学生几年级的,
再增加对应年级的学生总数。 mysql> delimiter $$
mysql> create trigger trigger_student_count_insert
-> after insert
-> on student_info for each row
-> begin
-> update student_count set student_count=student_count+1 where student_class=0;
-> update student_count set student_count=student_count+1 where student_class=new.student_class;
-> end
-> $$
Query OK, 0 rows affected (0.09 sec) mysql> delimiter;
创建触发器,在DELETE时首先减少学生总人数,然后判断删除的学生是几年级的,再减少对应年级的学生总数;
mysql> DELIMITER $$
mysql>
mysql> CREATE TRIGGER trigger_student_count_delete
->
-> AFTER DELETE
->
-> ON student_info FOR EACH ROW
->
-> BEGIN
->
-> UPDATE student_count SET student_count=student_count-1 WHERE student_class=0;
->
-> UPDATE student_count SET student_count=student_count-1 WHERE student_class= OLD.student_class;
->
-> END
->
-> $$
Query OK, 0 rows affected (0.15 sec) mysql>
mysql> DELIMITER ;
mysql> desc student_info//
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| stu_no | int(11) | NO | PRI | NULL | auto_increment |
| stu_name | varchar(255) | YES | | NULL | |
| student_class | int(11) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
向学生表中分别插入多条不同年级的学生信息,查看触发器是否起做用:
mysql> INSERT INTO student_info VALUES(NULL,'AAA',1),(NULL,'BBB',1),(NULL,'CCC',2),(NULL,'DDD',2),(NULL,'ABB',1),(NULL,'ACC',1);
Query OK, 6 rows affected (0.12 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from student_info;
+--------+----------+---------------+
| stu_no | stu_name | student_class |
+--------+----------+---------------+
| 1 | AAA | 1 |
| 2 | BBB | 1 |
| 3 | CCC | 2 |
| 4 | DDD | 2 |
| 5 | ABB | 1 |
| 6 | ACC | 1 |
+--------+----------+---------------+
6 rows in set (0.00 sec) mysql> select * from student_count;
+---------------+---------------+
| student_class | student_count |
+---------------+---------------+
| 1 | 4 |
| 2 | 2 |
| 0 | 6 |
+---------------+---------------+
3 rows in set (0.00 sec) 可以看到,总共插入6条数据,学生总数是6,1年级4个,2年级2个,trigger正确执行。
从学生表中分别岀除多条不同年级的学生信息,查看触发器是否起作用:
mysql> delete from student_info where stu_name like 'A%';
Query OK, 3 rows affected (0.15 sec) mysql> SELECT * FROM student_info;
+--------+----------+---------------+
| stu_no | stu_name | student_class |
+--------+----------+---------------+
| 2 | BBB | 1 |
| 3 | CCC | 2 |
| 4 | DDD | 2 |
+--------+----------+---------------+
3 rows in set (0.00 sec) mysql> select * from student_count;
+---------------+---------------+
| student_class | student_count |
+---------------+---------------+
| 1 | 1 |
| 2 | 2 |
| 0 | 3 |
+---------------+---------------+
3 rows in set (0.00 sec)
从学生表中将姓名以A开头的学生信息删除,学生信息删除的同时,数量表也跟随变化。
在上面的示例中,使用了三个新的关键字:DELIMITER、NEW、OLD,这三个关键字在官网上“触发器语法”一节中都有介绍

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

  1. mysql之触发器trigger 详解

    为了梦想,努力奋斗! 追求卓越,成功就会在不经意间追上你 mysql之触发器trigger 触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table)  ...

  2. mysql之触发器入门

    触发器语法: CREATE TRIGGER <触发器名称>  --触发器必须有名字,最多64个字符,可能后面会附有分隔符.它和MySQL中其他对象的命名方式基本相象.{ BEFORE |  ...

  3. Mysql笔记——触发器简单实例

    首先贴上触发器语法吧: CREATE TRIGGER <触发器名称> –触发器必须有名字,最多64个字符,可能后面会附有分隔符.它和MySQL中其他对象的命名方式基本相象. { BEFOR ...

  4. mysql的触发器

    删除触发器 drop TRIGGER 触发器名字; 查找库里面的所有触发器 SELECT * FROM information_schema.`TRIGGERS`;show triggers 触发器语 ...

  5. Mysql 视图,触发器,存储过程,函数,事务

    视图 视图虚拟表,是一个我们真实查询结果表,我们希望将某次查询出来的结果作为单独的一个表,就叫视图,无法对图字段内容进行增删改. --格式: CREATE VIEW 视图名字 AS 操作; --比如: ...

  6. MySql之触发器的使用

    一:触发器的使用场景 当数据库的记录发生变化时,自动触发某些操作. MySQL的触发器响应三种操作,六种场合: 三种操作:DELETE.INSERT.UPDATE. 六种场合:三种操作的BEFORE. ...

  7. (mysql)触发器、事件、事务、函数

    1.事务操作原理:事务开启之后Start transaction,所有的操作都会临时保存到事务日志.只有在得到commit才会关闭,否则清空:2.设置回滚点: savepoint 回滚点名字:  回到 ...

  8. MySQL数据库----触发器

    触发器-trigger 触发器:监视某种情况,并触发某种操作. 使用触发器可以定制用户对表进行[增.删.改]操作时前后的行为,注意:没有查询 -- 触发器:某种程序触发了工具的运行 -- 触发器不能主 ...

  9. MYSQL:基础——触发器

    MYSQL基础——触发器 引入触发器 什么是触发器 如果你想要某条语句(或某些语句)在事件发生时自动执行.比如: 每当订购一个产品时,都从库存数量中减去订购的数量:无论何时删除一行,都在某个存档表中保 ...

随机推荐

  1. 如何用命令行执行loadrunner的脚本

    SET M_ROOT=D:\Mercury Interactive\Mercury LoadRunner\bin cd %M_ROOT% wlrun.exe -TestPath D:\ceshi10\ ...

  2. Spring Boot 使用Jar打包发布, 并使用 Embedded Jetty/Tomcat 容器

    Jar包发布 在项目pom.xml中, 如果继承了Spring Boot的starter parent, 那么默认已经包含打包需要的plugins了, 设置为jar就能直接打包成包含依赖的可执行的ja ...

  3. Java中的String pool

    public String intern()返回字符串对象的规范化表示形式. 一个初始时为空的字符串池,它由类 String 私有地维护.当调用 intern 方法时,如果池已经包含一个等于此 Str ...

  4. 转:Ogre内部渲染流程

    以下是 Ogre 的代码中的详细说明: Renderable是OGRE中所有可渲染对象的抽象接口 这个接口抽象出了在渲染管线中的被分组的离散的可渲染对象基本的方法. 此接口的实现类必须是基于单一的材质 ...

  5. 收到新信息,弹出popup窗口提示

     

  6. Docker运行python容器

    容器是镜像运行的实例,而镜像保存在仓库里,测试或者发布生产环境只需要pull下来即可,相对传统的应用部署,能很好的保持环境的一致,节省运维时间.最近公司内部的java和.net服务也已经全部容器化,实 ...

  7. 倒计时实现两种方法-NSTimer/GCD

    #import "ViewController.h" @interface ViewController () @property (nonatomic ,strong)UIBut ...

  8. NSNotificationCenter实现原理

    # 前言 Cocoa中使用NSNotification.NSNotificationCenter和KVO来实现观察者模式,实现对象间一对多的依赖关系. 本篇文章主要来讨论NSNotification和 ...

  9. mount 需要同时设置 noatime 和 nodiratime 吗?

    相信对性能.优化这些关键字有兴趣的朋友都知道在 Linux 下面挂载文件系统的时候设置 noatime 可以显著提高文件系统的性能.默认情况下,Linux ext2/ext3 文件系统在文件被访问.创 ...

  10. 树莓派进阶之路 (020) - 基于24位AD转换模块HX711的重量称量实验

    参考文档:http://www.geek-workshop.com/thread-2315-1-1.html 参考文档:https://wenku.baidu.com/view/e5d5e4e2652 ...