简体   繁体   English

MySQL使用父键更新外键字段值

[英]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. 在我的数据库中,有表名session和session_id是它的主键。 and there is member table and member_id is the primary key. 并且有成员表,而member_id是主键。 also there is relationship between two tables. 两个表之间也有关系。 session_id is the foreign key of member table. session_id是成员表的外键。 I want to update member_id with the same value of session_id while insert value into session table. 我想在会话表中插入值时用相同的session_id值更新member_id。 anyone can help me with this?? 任何人都可以帮助我吗?

You can use a MySQL trigger to accomplish this: 您可以使用MySQL触发器来完成此操作:

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

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

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