简体   繁体   中英

mysql update values in foreign key field value with parent key

in my database there is table name session and session_id is the primary key of that. and there is member table and member_id is the primary key. also there is relationship between two tables. session_id is the foreign key of member table. I want to update member_id with the same value of session_id while insert value into session table. anyone can help me with this??

You can use a MySQL trigger to accomplish this:

CREATE TRIGGER update_trigger
AFTER INSERT ON session FOR EACH ROW
BEGIN
    UPDATE member
    SET member_id = NEW.session_id
END;

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