我应该使用CASCADE DELETE规则吗? [英] Should I use the CASCADE DELETE rule?
问题描述
Duplicate of: When/Why to use Cascading in SQL Server?
我一直很害怕使用DELETE CASCADE,但是随着我变得更加自信(懒惰:D) ,我在想它有多糟,是使用它的最佳实践,还是应该避免使用它并以老式的方式(使用存储过程)清理我的外键等?
I've always been too scared to use DELETE CASCADE, but as I get more confident (lazy :D), I'm thinking how bad can it be, is it best practise to use it or should I avoid it and clean up my foreign keys etc the old fashioned way (with stored procedures)?
推荐答案
ON DELETE CASCADE很好,但是仅当相关行确实是要删除的行的逻辑扩展名时。例如,DELETE ORDERS可以删除关联的ORDER_LINES,因为您显然想删除此顺序,该顺序由标题和一些行组成,因此可以。另一方面,DELETE CUSTOMER不应删除关联的ORDERS,因为ORDERS本身很重要,而不仅仅是客户的属性。
ON DELETE CASCADE is fine, but only when the dependent rows are really a logical extension of the row being deleted. For example, it's OK for DELETE ORDERS to delete the associated ORDER_LINES because clearly you want to delete this order, which consists of a header and some lines. On the other hand, DELETE CUSTOMER should not delete the associated ORDERS because ORDERS are important in their own right, they are not just attributes of a customer.
一种思考的方式大概是这样的:如果我发出DELETE X并且它也删除Y,我会快乐还是不快乐?而且,如果我发出DELETE X并被告知由于存在Y而无法删除X,我会为保护感到高兴吗?还是对不便感到恼怒?
One way to think about this is: if I issue DELETE X and it also deletes Y, will I be happy or unhappy? And if I issue DELETE X and am told "cannot delete X because Y exists" will I be glad of the protection, or irritated at the inconvenience?
这篇关于我应该使用CASCADE DELETE规则吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!