服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - MySQL 触发器的使用和理解

MySQL 触发器的使用和理解

2021-04-18 22:29编程江湖浪客 Mysql

这篇文章主要介绍了MySQL 触发器的使用和理解,帮助大家更好的理解和学习使用MySQL,感兴趣的朋友可以了解下

1.触发器是什么?

一类特殊的数据库程序,可以监视某种数据的操作(insert/update/delete),并触发相关的操作(insert/update/delete),保护数据的完整性。

个人理解就有点类似于java的观察者模式,一个对象变化,观察者也跟着做出响应。

mysql好像是从5.0以后开始支持触发器的。

2.创建触发器

创建触发器我将介绍两种方式:用语句创建,用navicat创建。

创建触发器的语法如下:

?
1
2
3
4
5
6
7
8
9
create trigger trigger_name trigger_time trigger_event on tb_name for each row trigger_stmt
trigger_name:触发器的名称
tirgger_time:触发时机,为before或者after
trigger_event:触发事件,为insertdelete或者update
tb_name:表示建立触发器的表明,就是在哪张表上建立触发器
trigger_stmt:触发器的程序体,可以是一条sql语句或者是用beginend包含的多条语句
所以可以说mysql创建以下六种触发器:
before insert,before delete,before update
after insert,after delete,after update

其中,触发器名参数指要创建的触发器的名字

before和after参数指定了触发执行的时间,在事件之前或是之后

for each row表示任何一条记录上的操作满足触发事件都会触发该触发器

创建多个执行语句的触发器:

?
1
2
3
4
5
create trigger 触发器名 before|after 触发事件
on 表名 for each row
begin
 执行语句列表
end

 new和old的使用:

 

触发器类型 new和old的使用
insert new代表新增的数据
update new代表更新后的数据,old代表更新前的数据
delete old代表要删除的数据

 

某一个字段可以用new/lod.字段名

接下来我们创建2个表用来测试:

stu表:主表(被观察者)

MySQL 触发器的使用和理解

?
1
2
3
4
5
6
7
8
drop table if exists `stu`;
create table `stu` (
`id` int(11) not null auto_increment comment 'id',
`name` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null comment '姓名',
`age` int(11) null default null comment '年龄',
`sort` int(11) null default null comment '排序字段',
primary key (`id`) using btree
) engine = innodb auto_increment = 18 character set = utf8mb4 collate = utf8mb4_general_ci row_format = dynamic;

stu_log表:触发器关联表(观察者)

MySQL 触发器的使用和理解

?
1
2
3
4
5
6
7
drop table if exists `stu_log`;
create table `stu_log` (
`id` int(11) unsigned not null auto_increment,
`name` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null,
`create_time` datetime(0) null default null,
primary key (`id`) using btree
) engine = innodb auto_increment = 7 character set = utf8mb4 collate = utf8mb4_general_ci row_format = dynamic;

现在我们假设有这样一个业务,在stu表新增和删除数据时,同步在stu_log中记录日志,记录name和时间。

如果不使用触发器,我们就需要编写代码来实现这个需求,但是触发器可以帮我们轻松的实现。

首先我们使用语句来创建一个insert触发器:

?
1
2
3
4
5
6
7
drop trigger if exists `insert_log`;
delimiter ;;
create trigger `add_log` after insert on `stu` for each row begin
insert into stu_log(name,create_time) values(new.`name`,now());
end
;;
delimiter ;

执行结果:

MySQL 触发器的使用和理解

然后我们再用navicat创建一个delete触发器:

step1:右键stu表,选择设计表----触发器

MySQL 触发器的使用和理解

step2:如图所示填选,选择删除前触发

MySQL 触发器的使用和理解

step3:在下方定义框内写执行语句,如图   记得点保存!

MySQL 触发器的使用和理解

语句:

?
1
2
3
begin
insert into stu_log(name,create_time) values(old.`name`,now());
end

3.使用触发器

测试一下:新增一条数据

?
1
insert into stu (name,age) values('李白',36)

查看stu表和stu_log表:

MySQL 触发器的使用和理解

MySQL 触发器的使用和理解

 如图,触发器已经生效了!

测试删除一条数据

?
1
delete from stu where name = '李白'

查看stu表和stu_log表:

MySQL 触发器的使用和理解

MySQL 触发器的使用和理解

如图,触发器已经生效了!

以上就是mysql 触发器的使用和理解的详细内容,更多关于mysql 触发器的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/godjoker/p/14442831.html

延伸 · 阅读

精彩推荐