How can removing a temporary evaluation job also remove its child results?

With enforcement enabled, ON DELETE CASCADE propagates deletion of a referenced parent to dependent children.

SQLite example and interpretation

Removing job one in the temporary fixture clears its result. Historical audit records with longer retention need a separate policy.

PRAGMA foreign_keys=ON;
CREATE TABLE j(id INTEGER PRIMARY KEY);
CREATE TABLE result(jid REFERENCES j(id) ON DELETE CASCADE);
INSERT INTO j VALUES(1);
INSERT INTO result VALUES(1);
DELETE FROM j WHERE id=1;
SELECT count(*) FROM result;

Result [[0]]

中文

删除一条临时评测任务时,如何同步清理它的子结果?

外键的 ON DELETE CASCADE 在启用外键执行时,把被引用父行的删除传播到依赖子行。

SQLite 示例与适用条件

示例在临时内存库中删除任务 1,它的结果随之消失。需要永久保留的审计记录应使用单独的保留策略。

PRAGMA foreign_keys=ON;
CREATE TABLE j(id INTEGER PRIMARY KEY);
CREATE TABLE result(jid REFERENCES j(id) ON DELETE CASCADE);
INSERT INTO j VALUES(1);
INSERT INTO result VALUES(1);
DELETE FROM j WHERE id=1;
SELECT count(*) FROM result;

结果 [[0]]

References / 参考来源

Keep reading / 继续阅读