data preparation:
create table employee ( num int(50), d_id int(50), name varchar(50), age int(50), sex varchar(50), homeadd varchar(50) );
insert into employee values(1,1001,'zhangsan ' , 26 is , ' NaN3 ' , ' Beijing ' ); INSERT INTO Employee values ( 2 , 1001 , ' Lisi ' , 24 , ' NV ' , ' hunan ' ); INSERT INTO Employee values ( . 3 , 1002 , ' wangwu ' , 25 , ' NaN3 ' ,'jiangsu'); insert into employee values(4,1004,'aric',15,'nan','yingguo');
select * from employee; create table department ( d_id int(50), d_name varchar(50), functione varchar(50), address varchar(50) );
insert into department values(1001,'keyanbu','yanfachanpin','3lou5hao'); insert into department values(1002,'shengchanbu','shengchanchanp','5louyiceng'); insert into department values(1003,'xiaoshoubu','cehuaxiaoshou','1louxiaoshoudating');
select * from department;
select * from employee;
select * from department;
==========================================================================
the Create the Trigger trigger name the before | the After trigger event on the table name for the each Row execute the statement; ------------------------------- --------------------------------------------- DELIMITER && the Create the trigger trigger is the name of the before | the After trigger event on the table name for the each Row the begin execution of a statement list End && DELIMITER; triggering event is the trigger conditions, including INSERT, Update , the Delete ; table name refers to the name of the table the trigger event operations;
Create a table:
create table trigger_time ( exec_time varchar(50) );
select * from trigger_time;
----------select now();
=================================================
Create a trigger:
create trigger dept_trig1 before insert on department for each row insert into trigger_time values ( now() );
===========================================
To demonstrate, first delete a record, and then added to it:
delete from department where d_id = 1003;
=========================================
Add just deleted records:
insert into department values(1003,'xiaoshoubu','cehuaxiaoshou','1louxiaoshoudating');
===================================================================
在department表insert时,触发器会被触发,我们查看下:
select * from trigger_time;
===========================================================
==========================================================
示例02:
创建第二个演示示例用的时间表:
create table trigger_time1 ( exec_time varchar(50) ); select * from trigger_time1;
==================================================================
创建第二个触发器:
delimiter && create trigger dept_trig2 after delete on department for each row begin insert into trigger_time1 values ( now() ); insert into trigger_time values ( now() ); end && delimi
================================================
执行删除语句:
delete from department where d_id = 1003;
==========================================
查看被删除记录的表以及2个时间表:
select * from department; select * from trigger_time; select * from trigger_time1;
=================================================================================================
查看触发器
1、查看数据库中所有触发器的信息:
show triggers;
========================================================================
2、在triggers表中查看触发器信息
mysql中所有触发器的定义都存在information_schema数据库下的triggers表中,查询triggers表,可以查询数据库中所有触发器的详细信息
select * from information_schema.triggers; /*查询所有*/
select * from information_schema.triggers where trigger_name = 'dept_trig1'; /*单个指定查询*/
注意:在激活触发器时,对触发器中的执行语句存在一些限制。而且触发器有问题,会阻止程序向下执行,而且数据不能回滚。
select * from information_schema.triggers; /*查询所有*/ select * from information_schema.triggers where trigger_name = 'dept_trig1'; select * from information_schema.triggers where trigger_name = 'dept_trig2';
==================================================
3、删除触发器
drop trigger 触发器名;
select * from information_schema.triggers; drop trigger dept_trig1; drop trigger dept_trig2;