简体   繁体   English

MariaDB 在没有外键关系的行上删除 CASCADE

[英]MariaDB On delete CASCADE on rows without foreign key relationship

I have a problem I've been cracking my head over the last few days.我有一个问题,在过去的几天里我一直在头疼。 I have a relational DB (mariaDB), which has two tables.我有一个关系数据库(mariaDB),它有两个表。 Table A and table B. Table A has a relation with itself, and when one row is deleted, multiple MAY be deleted (via on delete CASCADE).表A和表B。表A和自己有关系,当删除一行时,可能会删除多个(通过on delete CASCADE)。 Table B has no relation with table A. I cannot create a relation, as the rows currently present in the tables may or may not have the correct (to enforce this relation) data in it (I have no influence over this).表 B 与表 A 没有关系。我无法创建关系,因为表中当前存在的行可能有也可能没有正确的(强制这种关系)数据(我对此没有影响)。

When I delete a record from table A, I would like the implicitly related data from table B to also be deleted.当我从表 A 中删除一条记录时,我希望表 B 中的隐式相关数据也被删除。 As a foreign key cannot be created, a on delete CASCADE seems impossible.由于无法创建外键,因此删除 CASCADE 似乎是不可能的。 I thought of a trigger, which would find relevant rows on every delete from table A, however, triggers will not fire on rows deleted by a on delete CASCADE.我想到了一个触发器,它会在表 A 中的每次删除时找到相关行,但是,触发器不会在由 on delete CASCADE 删除的行上触发。 Meaning that when row X (from table A) gets deleted as a result of a CASCADE, the relevant data from table B will not be deleted.这意味着当 X 行(来自表 A)由于 CASCADE 被删除时,表 B 中的相关数据将不会被删除。

If I had to summarize this to a question: Is it possible to delete a row, like on delete CASCADE, without the use of foreign keys or triggers?如果我不得不将其总结为一个问题:是否可以在不使用外键或触发器的情况下删除一行,例如在删除 CASCADE 时?

Thanks for reading, appreciate it!感谢阅读,不胜感激!

Thanks to @Akina.感谢@Akina。

Iterate over rows relevant to the one deleted in the trigger, and then delete all rows relevant to those rows in table B. This works, but I cannot accept my own answer as of yet.迭代与触发器中删除的行相关的行,然后删除与表 B 中这些行相关的所有行。这可行,但我目前还不能接受我自己的答案。

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

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