mysql 触发器
语法:
l create trigger trigger_name
before | after
insert | update | delete
on table_name
for each row
begin
sql
end
create trigger:关键字
trigger_name:触发器名称(自定义)
trigger_time:触发时间,取值范围:before(之前)和after(之后)
trigger_event:触发事件,取值范围:insert、update、delete
on:关键字
table_name:表名
for each row:关键字
begin:开始(关键字)
sql:sql语句(可以有多条语句)
end:结束(关键字)
show triggers;
查询当前数据库下所有表的触发器
drop trigger trigger_name ;
删除触发器
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(20) CHARACTER SET utf8 NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1
CREATE TABLE `user_age` ( `id` int(11) NOT NULL AUTO_INCREMENT, `age` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1
mysql> delimiter // mysql> create trigger chufaqi -> after -> insert -> on user -> for each row -> begin -> insert into user_age (age) values ('123'); -> end -> //
运行 insert into user (username) values ('lclliang');