简体   繁体   中英

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

I have two tables:

  1. items
  2. items_details

If tabel 2 updates it's content, I want the timestamp (change on update) within tabel 1 updated.

Therefore I definded in MySql foreign key constraint on update cascade within table 2. This somehow has no effect at all.

How can the timestamp of table 1 be updated if the content in table 2 changes?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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