简体   繁体   English

如何在更新级联上使用mysql外键约束更新时间戳?

[英]How to update timestamp with mysql foreign key constraint on update cascade?

I have two tables: 我有两张桌子:

  1. items 项目
  2. items_details items_details

If tabel 2 updates it's content, I want the timestamp (change on update) within tabel 1 updated. 如果tabel 2更新了它的内容,我希望更新tabel 1中的时间戳(更新时更改)。

Therefore I definded in MySql foreign key constraint on update cascade within table 2. This somehow has no effect at all. 因此我在表2中的更新级联中对MySql外键约束进行了定义。这在某种程度上根本没有任何影响。

How can the timestamp of table 1 be updated if the content in table 2 changes? 如果表2中的内容发生变化,如何更新表1的时间戳?

You could use an after update trigger: 您可以使用更新后触发器:

DELIMITER //
CREATE TRIGGER items_details_after_update
AFTER UPDATE
ON items_details FOR EACH ROW
BEGIN
    UPDATE items
    SET timestamp = CURRENT_TIMESTAMP
    WHERE NEW.item_id = id;
END; //
DELIMITER ;

This answer assumes that there exists a column item_id in the item_details table, which is a foreign key pointing to a primary key id column in the parent items table. 此答案假定item_details表中存在列item_id ,该列是指向父items表中主键id列的外键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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