Need an abstract trigger in MySQL 5.1 to update an audit log(需要 MySQL 5.1 中的抽象触发器来更新审计日志)
问题描述
我需要一种方法来检查表中已更改的任何条目的条目并将其传递到审核日志中.它需要从表结构中抽象出来.
I need a way to check for and pass entries into an audit log for any entries in a table that have been changed. It needs to be abstracted away from the table structure.
例如:
CREATE TRIGGER table1_update 
BEFORE UPDATE ON table1 
FOR EACH ROW BEGIN
  DECLARE i_column_name varchar(32);
  DECLARE done INT;
  DECLARE cursor1 CURSOR FOR SELECT column_name FROM information_schema.columns WHERE table_name = 'table1';
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  OPEN cursor1;
  REPEAT
    FETCH cursor1 INTO i_column_name;
    IF NOT done THEN
      --pass the variable column_name and its old.i_column_name and new.i_column_name values to the audit table
    END IF;
  UNTIL done END REPEAT;
  CLOSE cursor1;
END$$
我们有太多的表需要审核才能自定义构建每个 INSERT、UPDATE 和 DELETE 触发器.我已经尝试了很多东西,但我想我不走运了.有人有什么想法吗?
We have too many tables that need to be audited to custom build every single INSERT, UPDATE, and DELETE trigger. I've tried a number of things and I'm thinking I'm out of luck. Anyone have any ideas?
推荐答案
不能有抽象触发器,它必须定义在特定的表上.最接近的就是把触发器的代码放到一个存储过程中,然后每个表的触发器就会调用这个过程.
You can't have an abstract trigger, it must be defined on a specific table. The closest you can get is to put the code for the trigger into a stored procedure, and then the triggers for each table will just call the procedure.
CREATE PROCEDURE audit_update (IN tablename VARCHAR(64))
BEGIN
  DECLARE  i_column_name varchar(32);
  DECLARE done INT;
  DECLARE cursor1 CURSOR FOR SELECT column_name FROM information_schema.columns WHERE table_name = tablename;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  OPEN cursor1;
  REPEAT
    FETCH cursor1 INTO i_column_name;
    IF NOT done THEN
      --pass the variable column_name and its old.i_column_name and new.i_column_name values to the audit table
    END IF;
  UNTIL done END REPEAT;
  CLOSE cursor1;
END
CREATE TRIGGER table1_update 
BEFORE UPDATE ON table1 
FOR EACH ROW BEGIN
  CALL audit_update('table1');
END
您应该能够轻松编写一些脚本,使用 information_schema 或类似内容为所有表创建触发器.
You should be able to easily script up something that will create the triggers for all of your tables using the information_schema or something along those lines.
SELECT CONCAT('CREATE TRIGGER ', table_name, '_update BEFORE UPDATE ON ', table_name, ...) FROM information_schema...
                        这篇关于需要 MySQL 5.1 中的抽象触发器来更新审计日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:需要 MySQL 5.1 中的抽象触发器来更新审计日志
				
        
 
            
        基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
 - MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
 - MySQL 5.7参照时间戳生成日期列 2022-01-01
 - 带有WHERE子句的LAG()函数 2022-01-01
 - 从字符串 TSQL 中获取数字 2021-01-01
 - CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
 - 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
 - while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
 - 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
 - ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				