Cascade Delete Query
I have three tables. Product, Company, Employee
ProductId of Product table is foregin key for Company and CompanyId of Company table is foregin key for Employee
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 a开发者_运维问答lter 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;
Try this option. I don't have environment to test this. I guess with some changes it should work at your end.
DELETE Product,Company,Employee
FROM
user LEFT JOIN items ON product.productid = company.productid
LEFT JOIN orders ON company.productid = product.productid
WHERE product.productid = [$productid]
精彩评论