级联删除查询 [英] Cascade Delete Query

查看:78
本文介绍了级联删除查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个桌子. 产品,公司,员工

I have three tables. Product, Company, Employee

ProductId是公司的forgin键 并且Company表的CompanyId是Employee的前瞻键

ProductId of Product table is foregin key for Company and CompanyId of Company table is foregin key for Employee

因此,从Product表中删除ProductId时,其他表中的所有相关记录都应删除.但是我不能碰模式(不能使用alter table).在这种情况下,我应该如何编写查询.

So on deleting ProductId from Product table, all the related records in other tables should delete. But I can't touch schema(can't use alter table). How should I write the query in that case..

推荐答案

如果您无法添加传播删除操作的约束,则必须自己编写所有必需的删除操作:

If you can't add constraints that propagates the delete, you have to write all the necessary deletes yourself:

delete employee where companyid in (select companyid from company c where productid = xxx);
delete company where productid=xxx;
delete product where productid=xxx;

这篇关于级联删除查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆