通过关系(外键)SQLite更新其他列值
本文关键字:更新 其他 SQLite 关系 外键 | 更新日期: 2023-09-27 18:34:53
当我们使用外键更改SQLite数据库中父表列中的值时,我们如何更改(更新(子表列值?
使用触发器:
CREATE TRIGGER parent_update
AFTER UPDATE OF primary_key ON parent_table
BEGIN
UPDATE child_table SET parent_key=NEW.primary_key WHERE parent_key=OLD.primary_key;
END;
一般公式如下:
1. Disable or remove the FK constraint.
2. Update the Parent PK, but keep a list of the Old_PK and the New_PK values.
3. Update the child records using the New_PK(s), but matching on the Old_PK values.
4. Enable or re-add the FK constraint.