简体   繁体   中英

How to trigger an action after an update in a MySQL table?

Basically I have something like:

TABLE PARENTS
...
name VARCHAR(45)
lastModified DATETIME
idModified INT
...

TABLE CHILDRENS
...
name VARCHAR(45)
parentName VARCHAR(45)
lastModified DATETIME
idModified INT
...

So what I want is when I perform:

UPDATE CHILDRENS SET lastModified=NOW(), idModified=123 WHERE NAME='someName';

The parent specified in parentName must also get updated with that same data.

I could do it manually, via the programming language I use, but that's not the point, I don't want to send another request to the database.


EDIT: I'm new to mysql, I don't know how to use triggers, oh I wrote childrens; bad grammar, sorry.

UNTESTED :

 CREATE TRIGGER triggers_name
AFTER UPDATE 
ON
CHILDRENS
FOR EACH ROW 
  UPDATE PARENTS,NEW
        SET PARENT.lastModified=NEW.lastModified,
            PARENT.idModified=NEW.idModified
        WHERE PARENT.name=NEW.parentName

hope it works now becouse i cant test it right now :/

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