mysql - 记录 mysql 更新

标签 mysql logging

我有一个从一个数据库读取并写入第二个数据库的迁移脚本。

我通常会更新现有记录。我如何记录更新,例如:

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

这意味着 productID 125 的标题为“Products1”,更新后变为“test”,价格为“125”,变为“140”

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

还有其他方法吗?

最佳答案

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

从我的头顶(以下假设 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 ;

关于mysql - 记录 mysql 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2666638/

相关文章:

entity-framework - 在 DB-First Approach 的调试窗口中记录所有 Entity Framework 查询

Python 的单元测试没有按预期初始化记录器

php - 使用php在mysql中制作表

PHP使用数组定义的where语句列名查询表

python - 为什么 Python 日志记录默认写入 stderr?

apache - CodeIgniter 解析错误显示但未写入日志

mysql - 如何使用编码密码转储数据库

php - 在MySQL数据库中保存图片路径,然后拉取该数据显示图片?

mysql - 实体关系图冗余 : store, 产品、订单、类别

php - 如何向 Symfony/Monolog 日志输出添加附加信息(主机、URL 等)?