如何一对多删除FK?

发布于 2024-07-14 17:17:42 字数 427 浏览 3 评论 0原文

我有一个表(aspnet_Membership),其中专门包含我需要删除的垃圾邮件记录。 有一些外键,我正在尝试编写一个简单的 SQL 语句来删除 FK,以便我可以删除主记录。

因此,有一个名为“aspnet_UsersInRoles”的表,其中包含 UserID 和 RoleID,而在我的“aspnet_Membership”表中是 UserID。 我无法在不孤立记录的情况下删除用户(由于限制无论如何也不会让我这样做)。

我基本上如何运行以下内容:

'delete from 'aspnet_UsersInRoles' where UserID in 'aspnet_Membership' and 'aspnet_Membership.CreateDate >= '03/15/2009'?

谢谢您的任何建议。

I have a table (aspnet_Membership) specifically that has spam records in it that I need to delete. There are some foreign keys and I'm trying to write a simple SQL statement to remove the FK's so I can delete the primary record.

So theres a table called 'aspnet_UsersInRoles' that has UserID and RoleID, and in my 'aspnet_Membership' table is the UserID. I can't delete the User without orphaning a record(wouldn't let me do that anyways due to contraints).

How can I essentially run the following:

'delete from 'aspnet_UsersInRoles' where UserID in 'aspnet_Membership' and 'aspnet_Membership.CreateDate >= '03/15/2009'?

Thank you for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沒落の蓅哖 2024-07-21 17:17:42

好吧,您可能已经设置了外键来执行级联删除,这样您就不需要担心它,但是您在查询中的尝试非常接近可行的查询,只需使用子查询即可:

DELETE FROM aspnet_UsersInRoles WHERE UserID IN (SELECT UserID FROM aspnet_Membership WHERE aspnet_Membership.CreateDate >= '03/15/2009')

Well, you probably could have set up the foreign keys to do a cascading delete, so that you didn't need to worry about it, but your try at the query was pretty close to one that would work, just use a subquery:

DELETE FROM aspnet_UsersInRoles WHERE UserID IN (SELECT UserID FROM aspnet_Membership WHERE aspnet_Membership.CreateDate >= '03/15/2009')
删除→记忆 2024-07-21 17:17:42

您可以更改约束以在删除时进行级联:
链接

否则,您可以查看 sys.foreign_keys 表以获取所有外来密钥使用主键并自动生成 sql 的键首先进行删除。

You can alter the constraints to do a cascade all upon deletion:
link

Otherwise you can look in the sys.foreign_keys table to get all the foreign keys using the primary key and auto generate sql to do deletes there first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文