如何一对多删除FK?
我有一个表(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您可能已经设置了外键来执行级联删除,这样您就不需要担心它,但是您在查询中的尝试非常接近可行的查询,只需使用子查询即可:
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:
您可以更改约束以在删除时进行级联:
链接
否则,您可以查看 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.