如何在SQL中使用Cascade Delete? [英] How to use Cascade Delete in SQL?

查看:155
本文介绍了如何在SQL中使用Cascade Delete?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



我现在有点困难,所以我也可能会问,我已经创建了一个2个表,其中有两列关系。当主键行被删除时,如何使用级联删除自动删除外键行?



我不知道这个东西的代码块如何工作,所有我看到的示例将表和列创建与代码块和其他代码混合在一起使我很难理解。

Hey guys,

I'm kinda stuck now so I might ask as well, I already created a 2 tables with 2 columns in a relationship. How can I use cascade delete to automatically delete the foreign key rows when the primary key row got deleted?

I dont get how the code block for this thing works, all the example I saw have the table and column creation mixed with the code block and other codes that makes it hard for me to understand.

推荐答案

这可能不是你的意思正在寻找,但我个人从不使用级联删除。



如果我确实发现需要将主键表删除为外键约束,我首先删除所有带有该外键的行







This may not be what you are looking for, but personally I never use cascading deletes.

If I do find the need to delete the primary key table to a foreign key constraint, I first just delete all the rows with that foreign key

i.e.

delete from customersales where customerId = 23
delete from customer where customerid = 23





这样我控制住了,我不用担心关于某人不小心删除了他们不想要的一大堆记录!



that way I am in control, and I needn't worry about someone accidentally deleting a whole bunch of records they didn't mean to!


这篇关于如何在SQL中使用Cascade Delete?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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