如何:当表 A 上的记录被删除时,表 B 上与表 A 关联的所有记录也应该被删除吗?
通过考虑以下方案:
我们需要确保,如果关联被删除,所有狗属于该协会的内容也应删除。
然而,在执行此操作时,保留 Association 和 Dog 表之间实际存在的关系是有意义的,因为每个关联可以有多个 Dog,但是,一只 Dog 只属于一个 Association。所以我相信外键配置是正确的。
我相信我应该在某个地方应用级联,但我不知道在哪里。 :(
请指教
By having into consideration the following scheme:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择“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).
要添加 Dog 和关联之间的关系: 在 Dog 属性上为引用association.id 的association_id 列添加一个新的外键。选择删除时
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).