Heim  >  Artikel  >  Datenbank  >  Mysql那些事儿之(十一)触发器二_MySQL

Mysql那些事儿之(十一)触发器二_MySQL

WBOY
WBOYOriginal
2016-06-01 13:39:00866Durchsuche

bitsCN.com


Mysql那些事儿之(十一)触发器二

 

相关链接:

Mysql那些事儿之(一)mysql的安装

http:///database/201210/162314.html;

Mysql那些事儿之(二)有关数据库的操作

http:///database/201210/162315.html;

Mysql那些事儿之(三)有关数据表的操作

http:///database/201210/162316.html;

Mysql那些事儿之(四)数据表数据查询操作

http:///database/201210/162317.html;

Mysql那些事儿之(五)操作时间

http:///database/201210/162318.html;

Mysql那些事儿之(六)字符串模式匹配

http:///database/201210/163969.html;

Mysql那些事儿之(七)深入select查询

http:///database/201210/163970.html;

Mysql那些事儿之(八)索引

http:///database/201210/163971.html;

Mysql那些事儿之(九)常用的函数

http:///database/201210/164229.html;

Mysql那些事儿之(十)触发器一

http:///database/201210/164516.html

 

比较after insert、before insert、after update、before update触发时间与事件 的触发情况。

     

Sql代码  

--创建表  

create table film_text(  

id smallint auto_increment,  

name varchar(40),  

txt text,  

primary key(id)  

);  

  

对于film表在 触发器 一 里有提过,代码就不写了。

针对表film 创建before insert 触发器:

 

Sql代码  

--创建before insert 触发器  

create trigger trigger_film_bef  --触发器名称为trigger_film_bef   

before insert --触发器的时间和事件  

on film   

for each row  --行级触发器  

begin  

   insert into film_text values(null,'sunny','before insert');  

end;  

针对表film 创建 after insert 触发器:

 

Sql代码  

--创建after insert 触发器  

create trigger trigger_film_aft  --触发器名称为trigger_film_aft  

after insert --触发器的时间和事件  

on film   

for each row  --行级触发器  

begin  

   insert into film_text values(null,'sunny','after insert');  

end;  

     

针对表film 创建 after update触发器

 

Sql代码  

--创建after update 触发器  

create trigger upd_film_aft  --触发器名称为upd_film_aft    

after update --触发器的时间和事件  

on film   

for each row  --行级触发器  

begin  

   insert into film_text values(null,'sunny','after update');  

end;  

 

针对表film 创建 before update触发器

 

Sql代码  

--创建before update触发器  

create trigger upd_film_bef  

before update on film  

for each row  

begin  

insert into film_text values(null,'sunny','before update');  

end;  

     

到现在四个触发器创建完了。

 

可以做下实验:

插入一条数据

 

Sql代码  

insert into film values(null,'sunny','i like you',null);  

查询表film_text:

 

Sql代码  

select * from film_text;  

Sql代码  

 结果如下:  

 

Sql代码  

mysql> select * from film_text;  

+----+---------+-----------------+  

| id | name    | txt             |  

+----+---------+-----------------+  

|  9 | huigui0 | thank you...... |  

| 10 | sunny   | before insert   |  

| 11 | sunny   | after insert    |  

+----+---------+-----------------+  

3 rows in set  

 说明了 before与after的顺序。

     

更新一条数据:

Sql代码  

update film set name='sunny' where id=1;  

 

结果如下:

Sql代码  

| 12 | sunny   | before update   |  

| 13 | sunny   | after update    |  

+----+---------+-----------------+  

5 rows in set  

 说明了 before与after的顺序。

 

bitsCN.com
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn