2010-04-19 85 views
1

我有一个迁移脚本,它从一个数据库读取并写入第二个数据库。日志mysql更新

我通常会更新现有记录。我如何才能登录,如更新:

productID : 125 
title : Product1 => test update 
price : 125 => 140 

这意味着,产品ID 125曾题“产品1”和更新后成为“试验”和有价“125”,这成了“140”

一个想法是读取记录保存的值,然后更新,再次读取值和比较并记录必要的字段。

是否存在其他方法?

回答

2

您可以使用触发器并将更改存储在另一个表中。

从我的头顶开始(以下假定productId永远不会更新);

create table main (
    `id` int not null auto_increment, 
    `title` varchar(30) not null, 
    `price` float not null, 
    primary key(`id`) 
); 

create table logger (
    `id` int not null auto_increment, 
    `productId` int not null, 
    `from_title` varchar(30) not null, 
    `to_title` varchar(30) not null, 
    `from_price` float not null, 
    `to_price` float not null, 
    primary key(`id`) 
); 

delimiter // 
create trigger my_logger before update on main 
begin 
    insert into 
     logger 
    set 
     `productId`=OLD.`id`, 
     `from_title`=OLD.`title`, 
     `to_title`=NEW.`title`, 
     `from_price`=OLD.`price`, 
     `to_price`=NEW.`title`; 
end;// 
delimiter ;