duankangzi766767 2014-01-09 08:22
浏览 57

如何在php中的数据库中为选定的表创建sql日志?

Case scenario :

Database : sample_db

Table's : table_1 ,table_2 , table_3 , table_4

All the table's has column row_id

I want to create sql queries log if there is any row insert/update/delete for row_id = 'some_value' for table_1 and table_3 for a sample_db

Edits :

Sample Example :

Let say there is a database school_db , which has following tables:

a.school. [school_id,school_name] 
b.class. [school_id,class_id,class_name]
c.section. [school_id,class_id,section_id,section_name]
d.marks. [school_id,student_id,marks]
e.exams. [school_id,class_id,exam_id,exam_name]

Now for a particular school's , let's say 'ABC school' which has school_id = 10 .

I want to log all the sql queries that are run for 'ABC school' for tables school,marks and exams.
  • 写回答

3条回答 默认 最新

  • doulan1866 2014-01-09 08:36
    关注

    You could use triggers in the database.

    Something like this:

    CREATE TRIGGER log_update AFTER UPDATE ON table_1
         FOR EACH ROW
         BEGIN
             IF NEW.row_id in ('some_value','some_other_value') THEN
                 INSERT INTO log .....;
             END IF;
         END;
    

    You replace the dots by somethign you want to insert into the log. Repeat this for table_3.

    Also repeat this to replace UPDATE with DELETE.

    You could use this to trigger on INSERT, but I don't see the point since row_id doesn't exist yet (assuming it's unique).

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了