举例二、
alvin1表存放了学生的信息。每次增加(insert)一个学生的信息。就触发一次统计。统计结果存入aac表里面;
首先创建一个alvin1表结构
create table alvin1( user_id int(10), username varchar(20), old tinyint(4), address varchar(30)); create table aac( my_count int);
然后开始创建一个触发器
delimiter&& create trigger alvin123 before insert on alvin1 for each row begin declare ycount int(10);#:申明变量类型 set ycount=(select count(*) from alvin1);#:给变量赋值 insert into aac(my_count) values(ycount);#:调用变量 end&& delimiter ;
看看before和after的区别
create trigger alvin123 after insert on
zyalvin1 for each row
begin
declare ycount int(10);
set ycount=(select count(*) from zyalvin1);
insert into aac(my_count)values(ycount);
end&&
root@zytest 16:24>insert into alvin1 values('1001','zhangsan','18','China');开始测试
root@zytest 16:24>select * from aac;查看触发器统计的结果。3》查看触发器:
1> 查看所有触发器,提前要进入某库
#: show triggers \G;
2>在triggers表中查看触发信息
root@zytest 11:20>use information_schema;
root@zytest 11:19>select * from information_schema.triggers \G;
小技巧:所有触发器的信息都存在information_schema库中的triggers表里面,在使用select 查询单个触发器的时候。可以根据triggers表里面的字段名称
Trigger_name字段进行查询。
root@information_schema 11:24>select * from triggers where trigger_name='alvin1'\G;
4》删除触发器:
语法:
1>删除alvin1触发器
root@(none) 12:18>use zytest;
Database changed
root@zytest 12:18>drop trigger alvin1;
Query OK, 0 rows affected (0.03 sec)以上就是MySQL中关于索引与触发器详解的详细内容,更多请关注php中文网其它相关文章!
……