当前位置:网站首页>MySQL 进阶 触发器 -- 触发器介绍、触发器语法、触发器案例
MySQL 进阶 触发器 -- 触发器介绍、触发器语法、触发器案例
2022-04-22 20:42:00 【CodeJiao】
文章目录
1. 触发器介绍
触发器是与表有关的数据库对象,指在insert / update / delete之前(BEFORE)或之后(AFTER),触发并执行触发器中定义的SQL语句集合。触发器的这种特性可以协助应用在数据库端确保数据的完整性,日志记录,数据校验等操作 。
使用别名OLD和NEW来引用触发器中发生变化的记录内容,这与其他的数据库是相似的。现在触发器还只支持行级触发,不支持语句级触发。
| 触发器类型 | NEW 和 OLD |
|---|---|
| INSERT 型触发器 | NEW 表示将要或者已经新增的数据 |
| UPDATE 型触发器 | OLD 表示修改之前的数据,NEW 表示将要或已经修改后的数据 |
| DELETE 型触发器 | OLD 表示将要或者已经删除的数据 |
2. 触发器语法
2.1 创建触发器
CREATE TRIGGER trigger_name
BEFORE/AFTER INSERT/UPDATE/DELETE
ON tbl_name FOR EACH ROW -- 行级触发器
BEGIN
trigger_stmt;
END;
2.2 查看触发器
SHOW TRIGGERS;
2.3 删除触发器
-- 如果没有指定 schema_name,默认为当前数据库
DROP TRIGGER [schema_name.]trigger_name ;
3. 触发器案例
通过触发器记录 tb_user 表的数据变更日志,将变更日志插入到日志表user_logs中, 包含增加、修改、删除;
表结构准备:
-- 准备工作 : 日志表 user_logs
create table user_logs
(
id int(11) not null auto_increment,
operation varchar(20) not null comment '操作类型, insert/update/delete',
operate_time datetime not null comment '操作时间',
operate_id int(11) not null comment '操作的ID',
operate_params varchar(500) comment '操作参数',
primary key (`id`)
) engine = innodb
default charset = utf8;
示例数据准备:
create table tb_user
(
id int primary key auto_increment comment '主键',
name varchar(50) not null comment '用户名',
phone varchar(11) not null comment '手机号',
email varchar(100) comment '邮箱',
profession varchar(11) comment '专业',
age tinyint unsigned comment '年龄',
gender char(1) comment '性别 , 1: 男, 2: 女',
status char(1) comment '状态',
createtime datetime comment '创建时间'
) comment '系统用户表';
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('吕布', '17799990000', '[email protected]', '软件工程', 23, '1', '6', '2001-02-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('曹操', '17799990001', '[email protected]', '通讯工程', 33, '1', '0', '2001-03-05 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('赵云', '17799990002', '[email protected]', '英语', 34, '1', '2', '2002-03-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('孙悟空', '17799990003', '[email protected]', '工程造价', 54, '1', '0', '2001-07-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('花木兰', '17799990004', '[email protected]', '软件工程', 23, '2', '1', '2001-04-22 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('大乔', '17799990005', '[email protected]', '舞蹈', 22, '2', '0', '2001-02-07 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('露娜', '17799990006', '[email protected]', '应用数学', 24, '2', '0', '2001-02-08 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('程咬金', '17799990007', '[email protected]', '化工', 38, '1', '5', '2001-05-23 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('项羽', '17799990008', '[email protected]', '金属材料', 43, '1', '0', '2001-09-18 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('白起', '17799990009', '[email protected]', '机械工程及其自动 化', 27, '1', '2', '2001-08-16 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('韩信', '17799990010', '[email protected]', '无机非金属材料工 程', 27, '1', '0', '2001-06-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('荆轲', '17799990011', '[email protected]', '会计', 29, '1', '0', '2001-05-11 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('兰陵王', '17799990012', '[email protected]', '工程造价', 44, '1', '1', '2001-04-09 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('狂铁', '17799990013', '[email protected]', '应用数学', 43, '1', '2', '2001-04-10 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('貂蝉', '17799990014', '[email protected]', '软件工程', 40, '2', '3', '2001-02-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('妲己', '17799990015', '[email protected]', '软件工程', 31, '2', '0', '2001-01-30 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('芈月', '17799990016', '[email protected]', '工业经济', 35, '2', '0', '2000-05-03 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('嬴政', '17799990017', '[email protected]', '化工', 38, '1', '1', '2001-08-08 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('狄仁杰', '17799990018', '[email protected]', '国际贸易', 30, '1', '0', '2007-03-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('安琪拉', '17799990019', '[email protected]', '城市规划', 51, '2', '0', '2001-08-15 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('典韦', '17799990020', '[email protected]', '城市规划', 52, '1', '2', '2000-04-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('廉颇', '17799990021', '[email protected]', '土木工程', 19, '1', '3', '2002-07-18 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('后羿', '17799990022', '[email protected]', '城市园林', 20, '1', '0', '2002-03-10 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES ('姜子牙', '17799990023', '[email protected]', '工程造价', 29, '1', '4', '2003-05-26 00:00:00');
3.1 插入数据触发器
create trigger tb_user_insert_trigger
after insert
on tb_user
for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params)
VALUES (null, 'insert', now(), new.id,
concat('插入的数据内容为: id=', new.id, ',name=', new.name, ', phone=', NEW.phone, ', email=', NEW.email,
', profession=', NEW.profession));
end;
测试:
-- 查看
show triggers;

-- 插入数据到tb_user
insert into tb_user(id, name, phone, email, profession, age, gender, status, createtime)
VALUES (26, '二皇子', '18809091212', '[email protected]', '软件工程', 23, '1', '1', now());
查看user_logs里面的数据

测试完毕之后,检查日志表中的数据是否可以正常插入,以及插入数据的正确性。
3.2 修改数据触发器
create trigger tb_user_update_trigger
after update
on tb_user
for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params)
VALUES (null, 'update', now(), new.id,
concat('更新之前的数据: id=', old.id, ',name=', old.name, ', phone=', old.phone, ', email=', old.email,
', profession=', old.profession, ' | 更新之后的数据: id=', new.id, ',name=', new.name, ', phone=',
NEW.phone, ', email=', NEW.email, ', profession=', NEW.profession));
end;
-- 查看
show triggers;

-- 更新
update tb_user
set profession = '会计'
where id = 23;
update tb_user
set profession = '会计'
where id <= 5;
测试完毕之后,检查日志表中的数据是否可以正常插入,以及插入数据的正确性。

3.3 删除数据触发器
create trigger tb_user_delete_trigger
after delete
on tb_user
for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params)
VALUES (null, 'delete', now(), old.id,
concat('删除之前的数据: id=', old.id, ',name=', old.name, ', phone=', old.phone, ', email=', old.email,
', profession=', old.profession));
end;
查看新增的删除数据触发器:
show triggers;

-- 删除数据
delete
from tb_user
where id = 26;
测试完毕之后,检查日志表中的数据是否可以正常插入,以及插入数据的正确性。

4. 小结

版权声明
本文为[CodeJiao]所创,转载请带上原文链接,感谢
https://blog.csdn.net/I_r_o_n_M_a_n/article/details/124342082
边栏推荐
- Self built CA center to issue certificates for different applications of the company
- 未授权访问漏洞总结
- Better implementation ideas based on MySQL in some scenarios (continuous update)
- 华为机试题——HJ72 百钱买百鸡问题
- 期货在网上直接开户,是否安全呢?
- SCI/SSCI期刊列表已更新,这几本期刊被剔除~
- [interview ordinary people vs Expert Series] please talk about the network quadruple
- (L2-026)小字辈(带权并查集)
- October's Android interview failed miserably in byte three, and fortunately won Xiaomi offer
- Ziguang Guowei: it is actively expanding the category of analog chips, and some products have been sold in batches
猜你喜欢

How to ensure the consistency between cache and database?

MATLAB学习笔记 - 计算特征向量手动执行PCA

Recommended chrome plug-ins

Virtual machine building and installation pulsar environment tutorial (for development and testing)

Ziguang Guowei: it is actively expanding the category of analog chips, and some products have been sold in batches

动态数据库工具——Database Inspector

2022年土建施工员题库精准小题库建设厅施工员

Windows install redis

MySQL主从复制之半同步复制
转载:程序员的发展方向
随机推荐
期货在网上直接开户,是否安全呢?
Windows安装Redis
MySQL alter best practices summary
Perfect forwarding implementation mechanism
Adobe系列错误代码解决方案汇总
String. Join() and stringutils Join () gracefully solves the splicing of arrays or collections
Short link design and thinking
【dfs】386. 字典序排数
Write a gateway service, understand more thoroughly!
Huawei machine test question -- hj72 100 money to buy 100 chickens
FPGA中的D触发器
day29
Error running ‘JeecgSystemApplication‘: Command line is too long. Shorten command line for JeecgSyst
Xshell7、Xftp7、Xlpd7-下载与安装教程(亲测可用)
普通函数做友元(用举例了解友函数)
MySQL高可用架构设计分析
MySQL Chapter 8 Database Constraints
CDH6.3.2 启用Kerberos 认证
MATLAB学习笔记 - 计算特征向量手动执行PCA
List的使用