当前位置:网站首页>MySql 触发器
MySql 触发器
2022-06-10 23:40:00 【Mr_Jin.】
当你想要监控一个表的数据操作时,就需要创建触发器。通过触发器得到对应的的增、删、改的操作信息。
一: 表 USER
表USER为用户信息表,以此表进行触发器监控表的的各种操作。
二:创建USER表的字表,用来存储USER表的操作信息及时间。
创建子表,包含操作的id,时间,操作状态 等。
create table t_change_log_t_USER(
id varchar(255) PRIMARY KEY ,
create_time DATETIME,
push_status int,
event_type varchar(20)
) ENGINE=INNODB;触发器的基本语法
CREATE TRIGGER <trigger_name>
BEFORE|AFTER
INSERT|UPDATE|DELETE ON <table_name>
FOR EACH ROW
BEGIN
sql语句
END<trigger_name> :触发器的名称
before|after:在事件之前处罚还是在事件之后处罚
insert|update|delete: 你需要触发的事件点
<table_name>:表名
for each row:固定写法
进行创建触发器 子表链接USER表
注:如果新增,更改,删除等操作都存在一张表中时,子表不要创建主键。不然id唯一存不进去。
更改操作:
CREATE TRIGGER trigger_update_log_t_USER
AFTER UPDATE ON USER
FOR EACH ROW
begin
Insert into t_change_log_t_USER(id, create_time, push_status,event_type) VALUES (NEW.id, NOW(), 0,'更新');
end;新增操作:
CREATE TRIGGER trigger_insert_log_t_USER
AFTER INSERT ON t_USER
FOR EACH ROW
begin
Insert into t_change_log_t_USER(id, create_time, push_status,event_type) VALUES (NEW.id, NOW(), 0,'新增');
end;
删除操作:
CREATE TRIGGER trigger_delete_log_t_USER
AFTER DELETE ON USER
FOR EACH ROW
begin
Insert into t_change_log_t_USER(id, create_time, push_status,event_type) VALUES (OLD.id, NOW(), 0,'删除');
end;当然在linux中操作数据库还有地方需要注意:因为此sql中有两个‘;’分号。执行时会出错。所以需要加上定义分界符DELIMITER // 在sql语句前加 DELIMITER // 在最后也要以// 结束。
例如:
delimiter //
CREATE TRIGGER trigger_update_log_t_USER
AFTER UPDATE ON USER
FOR EACH ROW
begin
Insert into t_change_log_t_USER(id, create_time, push_status,event_type) VALUES (NEW.id, NOW(), 0,'更新');
end;
//最后管理表进行查询就可得到具体信息,或者直接在子表中存储想要的信息都可。
其他
Linux mysql 拉取数据:
mysqldump -h ip地址 -uroot -p 数据库名 > 数据库名.sql
--skip-lock-tables >/home/nhxzf/存放路径地址/数据库名.sql
mysqldump -h 127.0.0.1 -uroot -p nhxzf > nhxzf.sql --skip-lock-tables >/home/nhxzf/nhxzf_data_push/nhxzf.sql边栏推荐
- 微信小程序实现OCR扫描识别
- ts+fetch实现选择文件上传
- How to install mathtype6.9 and related problem solutions in office2016 (word2016)
- [network counting] 1.4 network delay, packet loss and throughput
- Complete uninstallation of MySQL under Linux
- Safety training management measures
- Dual wing layout
- MP framework basic operation (self use)
- [network planning] 2.1.1 brief introduction to client server system and P2P system
- Brief introduction to MySQL lock and transaction isolation level
猜你喜欢

Safety training management measures

Lucene mind map makes search engines no longer difficult to understand

动态规划经典题目三角形最短路径

市值215亿,这个四川80后会让电视机成为历史?

Idea setting background picture (simple)
![[network counting] 1.4 network delay, packet loss and throughput](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network counting] 1.4 network delay, packet loss and throughput

哈工大软件构造复习——LSP原则,协变和逆变

跳转页面后回不去默认页面

Automated test series

Décomposition détaillée du problème de chemin le plus court du graphique
随机推荐
如何在office2016(word2016)中安装mathtype6.9及相关问题解决方案
BGP basic concept and iBGP basic configuration
五大类型负载均衡的原理场景详解
阻塞队列 — DelayedWorkQueue源码分析
Kwai handled more than 54000 illegal accounts: how to crack down on illegal accounts on the platform
Unable to return to the default page after page Jump
teterttet
圖的最短路徑問題 詳細分解版
[network planning] 2.2.3 user server interaction: cookies
The mystery of number idempotent and perfect square
系统应用安装时,签名校验失败问题
What is MYCAT? Get to know you quickly
Yii2 activerecord uses the ID associated with the table to automatically remove duplicates
学习笔记:插件化Activity之Hook点位
负载均衡策略图文详解
The driver has not received any packets from the server
Multipass Chinese documentation - Tutorial
Introduction and basic construction of kubernetes
JVM garbage collection mechanism and common garbage collectors
day01