Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL Trigger Update Copy Entire Row

Tags:

mysql

triggers

I am trying to create a trigger to copy an entire row to an audit table on any UPDATE.

I have 2 tables

Frequencies and Frequencies_Audit

This is my trigger.

create trigger auditlog 
before update on frequencies
for each row insert into
frequencies_audit select frequencies.*;

When I update the record I get Unknown Table frequencies.

I don't want to have to enter each field name separately if possible since we are constantly adding columns in the database.

like image 644
JeffD Avatar asked Jul 25 '12 03:07

JeffD


1 Answers

Rather than BEFORE UPDATE, you can write AFTER UPDATE trigger as follow ::

DELIMITER //
CREATE TRIGGER auditlog AFTER UPDATE ON frequencies
FOR EACH ROW BEGIN  
    INSERT INTO frequencies_audit select * from frequencies where freqId = NEW.freqId;
END;//
DELIMITER ;

freqId is just a name of Id column. Replace it with the name of Id column in your frequencies table.

like image 197
hemu Avatar answered Oct 25 '22 17:10

hemu