简体   繁体   中英

trigger for audit trail table

Im trying to create an audit trail for my "exam" table that shows any changes that are made to the grade on the table.

So i am stuck on creating the trigger that shows the username and date that changes the current grade.

I have been looking around but cant find anything. Any help will be really appreciated.

The original table:

      CREATE TABLE exam (
      student_no INT NOT NULL,
      exam_code CHAR(2) NOT NULL,
      grade TINYINT NULL,
      FOREIGN KEY (student_no) REFERENCES student (student_no),
      FOREIGN KEY (exam_code) REFERENCES course (exam_code));

Audit table i created:

      CREATE TABLE Audit (
      student_no INT NOT NULL,
      exam_code CHAR(2) NOT NULL,
      old_grade TINYINT NULL,
      updated_grade TINYINT NULL,
      Current_username VARCHAR(30),
      Date_updated DATETIME
      );


      CREATE TRIGGER audit_trail
      ??????

Below trigger can be an option.

CREATE TRIGGER audit_trail
AFTER UPDATE ON exam
FOR EACH ROW 
BEGIN 
INSERT INTO Audit values(OLD.student_no,OLD.exam_code,OLD.grade,NEW.grade,CURRENT_USER(), CURRENT_TIMESTAMP);  
END;

After any update in exam table, the above trigger will insert all associated information to Audit table.

You can check the demo here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM