Home > Database > Mysql Tutorial > MySql_数据库触发器的使用_MySQL

MySql_数据库触发器的使用_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:35:31
Original
919 people have browsed it

bitsCN.com

MySql数据库中的触发器使用:

触发器使用于insert/delete/update的前面或者后面(after/before)执行的sql语句。

创建触发器需注意要加上delimiter分隔符,以//开始中间写触发器以//结尾。

创建一个user1表

create table user1(
id int PRIMARY key AUTO_INCREMENT,
username varchar(20),
createtime date);
再创建一个user_log表
create table user_log(
id int PRIMARY key AUTO_INCREMENT,
ext int);

创建一个触发器(insert后执行的触发器)
delimiter//
create trigger trg_user_insert after insert on user1
 for EACH row
begin
 insert into user_log(ext) values(NEW.id);  new.id意为把user1表中的id传入user_log表中的ext中
end;
//

测试:插入user1表中数据后会自动执行触发器
insert into user1 values(null,'zhangsan11',now());
删除这个触发器的方法

DROP TRIGGER trg_user_insert;
Copy after login

 触发器中new与old:

在insert中有new的虚拟表,在delete中有old的虚拟表,在update中有new和old的虚拟表。

触发器中before:

常用于update中在修改后if ....then....end if

学艺不精,暂理解这么多,如有误请回复2013-04-09 16:58:23

 

 

bitsCN.com
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template