如何:当表 A 上的记录被删除时,表 B 上与表 A 关联的所有记录也应该被删除吗?

发布于 2024-11-04 02:25:53 字数 294 浏览 6 评论 0原文

通过考虑以下方案:

tablerelations

我们需要确保,如果关联被删除,所有狗属于该协会的内容也应删除。

然而,在执行此操作时,保留 Association 和 Dog 表之间实际存在的关系是有意义的,因为每个关联可以有多个 Dog,但是,一只 Dog 只属于一个 Association。所以我相信外键配置是正确的。

我相信我应该在某个地方应用级联,但我不知道在哪里。 :(

请指教

By having into consideration the following scheme:

table relations

We need to make sure that, if an association gets deleted, all the dogs that belong to that association, should also be deleted.

However, it makes sense to, while doing this, keep the relation that actually exists between Association and Dog tables, because, each association can have several Dogs, however, one Dog belong to only one Association. So I believe the foreign key configuration is correct.

I believe I should apply Cascade somewhere, but I'm not seeing where. :(

Please advice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雅心素梦 2024-11-11 02:25:53

选择“Dog”作为目标表,然后选择“外键”选项卡,您应该为所选的“association_id”字段选择一个外键。一旦您发现只需检查“外键选项”部分中显示的“删除时”弹出值,它应该显示“CASCADE”(类似于上面的屏幕截图显示的内容 - 如果没有显示“CASCADE”,只需选择它)。

Selecting 'Dog' as your target table, then selecting the 'Foreign Keys' tab, you should have a foreign key there for the 'association_id' field selected. Once you find that just check the popup value showing in the 'Foreign Key Options' section for 'On Delete' it should show 'CASCADE' (similar to what your screenshot above is showing - if it isn't showing 'CASCADE' simply select it).

孤城病女 2024-11-11 02:25:53

要添加 Dog 和关联之间的关系: 在 Dog 属性上为引用association.id 的association_id 列添加一个新的外键。选择删除时CASCADE

您也可以在查询窗口中执行这些步骤(就个人而言,我仅在需要打印数据库结构时才使用图形工具)。

ALTER TABLE Dog ADD CONSTRAINT `FK_byAssociationIdDog` 
 FOREIGN KEY(association_id)  
REFERENCES Association(id) ON UPDATE CASCADE ON DELETE CASCADE;

To add a relation between Dog and association: On Dog properties add a new foreign key for association_id column that refers association.id. Choose On Delete CASCADE.

You can also do these steps in query window as well (personally, I use graphical tools only if I need to print database structure).

ALTER TABLE Dog ADD CONSTRAINT `FK_byAssociationIdDog` 
 FOREIGN KEY(association_id)  
REFERENCES Association(id) ON UPDATE CASCADE ON DELETE CASCADE;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文