且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在多对多表上级联删除

更新时间:2023-02-02 22:33:01

你需要决定你想要的系统行为.您的要求听起来有点不正常,可能表明数据库架构设计中存在错误.为什么在删除相关文件夹时要删除项目?如果有另一个文件夹仍然与该项目相关,因为它是多对多关系怎么办?在这种情况下,删除 Item 实际上会导致 Item 和 FolderItem 之间的外键冲突.如果项目确实属于特定文件夹,也就是一对多关系,则您根本不需要 FolderItem 表.

You need to decide what behavior you want exactly with the system. Your requirement sounds a bit abnormal and might indicate a mistake in db schema design. Why do you want to delete an Item when a related Folder is deleted? What if there is another Folder still related to that item, since it is a many-to-many relationship? In that case, deleting the Item will actually cause foreign key violation between Item and FolderItem. If the Items actually do belong under a specific Folder, aka one to many relationship, you will not need the FolderItem table at all.

我想最有可能的情况是,如果没有其他 FolderItem 条目与之相关,您想删除该项目.在这种情况下,触发器是合适的解决方案,但您需要确保在触发器逻辑中检查它.

I guess the mostly likely case is you want to delete the Item if there is no other FolderItem entries related to it. In that case, trigger is the appropriate solution, but you need to make sure you are checking for it in the trigger logic.