Mysql那些事儿之(十一)触发器二_MySQL
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

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲
