开发者

MySQL - change tracking of the *data* in the DB?

I have a situation where I need to keep track of all changes to the data in a MySQL database.

For example, we have a field in the "customers" table which will contain a rating that indicates how risky it is to do business with that customer. Whenever this field is changed, I need to have it logged so we can go back and say "well they were a 3 and now they are an 8," for exampl开发者_如何学Ce. Is there any automated way to handle this in MySQL or am I just going have to write tons of change tracking logic into the application itself?


This is the type of thing that triggers are designed for inside of MySQL assuming you're using a 5+ version of MySQL.

CREATE TRIGGER log_change_on_table BEFORE UPDATE ON customers
    FOR EACH ROW
        BEGIN
            INSERT INTO customer_log (customer_id, rating, date)
                VALUES (OLD.customer_id, OLD.rating, now())
        END $$
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜