dongzhao5970 2016-03-16 17:41
浏览 62
已采纳

用于数据审计的MySQL Trigger或PHP? [关闭]

I am looking to keep the history of every single update of the tables, on my application. I am using Laravel, I know there is some package that can help me but I am looking something clean and fast.

I have two options:

  • MySQL Trigger
  • Via PHP code

My first question is, which one is faster ?

My second question is: should I use a schema like this and store all my tables into it:

CREATE TABLE revisions (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `revisionable_type` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
    `revisionable_id` INT(11) NOT NULL,
    `user_id` INT(11) NULL DEFAULT NULL,
    `key` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
    `old_value` TEXT NULL COLLATE 'utf8_unicode_ci',
    `new_value` TEXT NULL COLLATE 'utf8_unicode_ci',
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL
)

Or should I recreate a "duplicate" of every table like user_history and add the history on the respectives columns

Users: id, name, surname ...
Users_history: id, user_id, name, surname ... 
  • 写回答

2条回答 默认 最新

  • doushan2311 2016-03-16 18:06
    关注

    Given this schema:

    CREATE TABLE contacts (
        `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
        `first_name` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
        `last_name` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
        `updated_by` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
        `updated_at` TIMESTAMP NULL DEFAULT NULL
    )
    

    I would create this schema as well:

    CREATE TABLE contacts_audit (
        `id` INT(10) UNSIGNED NOT NULL,
        `first_name` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
        `last_name` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
        `updated_by` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
        `updated_at` TIMESTAMP NULL DEFAULT NULL,
        `action` varchar(255) NOT NULL COLLATE 'utf8_unicode_ci'
    )
    

    Then use these triggers (hopefully my trigger syntax isn't too rusty):

    CREATE TRIGGER contacts_audit_insert
    AFTER INSERT
       ON users FOR EACH ROW
    BEGIN
        INSERT INTO contacts_audit (id, first_name, last_name, updated_by, updated_at, action) values (NEW.id, NEW.first_name, NEW.last_name, NEW.updated_by, NEW.updated_at, 'insert');
    END;
    
    CREATE TRIGGER contacts_audit_update
    AFTER UPDATE
       ON users FOR EACH ROW
    BEGIN
        INSERT INTO contacts_audit (id, first_name, last_name, updated_by, updated_at, action) values (NEW.id, NEW.first_name, NEW.last_name, NEW.updated_by, NEW.updated_at, 'update');
    END;
    
    CREATE TRIGGER contacts_audit_delete
    BEFORE DELETE
       ON users FOR EACH ROW
    BEGIN
        INSERT INTO contacts_audit (id, first_name, last_name, updated_by, updated_at, action) values (OLD.id, OLD.first_name, OLD.last_name, OLD.updated_by, OLD.updated_at, 'delete');
    END;
    

    Potential drawbacks

    If you are using one MySQL username/password for database access and authenticating your users against a users table in MySQL then prior to performing any deletes, you will want to perform an update, within PHP, followed by the delete so that you can capture who deleted your record.

    However if you are using MySQL for authentication of all users then you can have this in your triggers: Take notice to the use of USER()

    INSERT INTO contacts_audit (id, first_name, last_name, updated_by, updated_at, action) values (NEW.id, NEW.first_name, NEW.last_name, USER(), NEW.updated_at, 'insert');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错