wudaokouer 2020-04-16
本文实例讲述了mysql触发器原理与用法。分享给大家供大家参考,具体如下:
首发日期:2018-04-14
create trigger trigger_addUserTime before insert on user_info for each row insert into usercreatetime(create_time) values(now());
create table user_info( id int primary key auto_increment, username varchar(20) not null, password varchar(20) not null ) create table usercreatetime( id int primary key auto_increment, create_time datetime ); create trigger trigger_addUserTime before insert on user_info for each row insert into usercreatetime(create_time) values(now()); insert into user_info(username,password) values("admin","admin888"); select * from usercreatetime;
delimiter $$ -- 一般定义成$$ create trigger 触发器名 before|after 事件 on 表名 for each row begin 语句;语句; end $$ delimiter ; -- 示例 delimiter $$ -- 一般定义成$$ create trigger trigger_addUserTime23 before insert on user_info for each row begin insert into usercreatetime(create_time) values(now()); insert into usercreatetime(create_time) values(now()); end $$ delimiter ;
create table student( id int primary key auto_increment, name varchar(15) not null, gender varchar(15) not null ); create table stu_info( name varchar(15) not null ); create trigger addCount before insert on student for each row insert into stu_info values(new.name); insert into student(name,gender) values("lilei","male"); select * from stu_info;
更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL查询技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》、《MySQL数据库锁相关技巧汇总》及《MySQL常用函数大汇总》
希望本文所述对大家MySQL数据库计有所帮助。