检查MySQL中是否可以删除一行
有没有办法检查一行是否可以被删除?例如,它当前没有通过受限制的外键连接到其他任何东西。
原因:我正在制作一个管理页面,其中列出了系统中的所有用户。它们始终可以被禁用,但也可以被删除。然而,只有当它们没有连接到任何关键的东西时才可以被删除。如果可以在数据库中轻松完成,我希望不必手动检查。
注意:我不想实际删除任何用户。我只想向管理员显示用户可以被删除。
Is there a way I can check if a row potentially could be deleted? That it for example is not currently connected through restricted foreign keys to anything else.
Reason: I am making an admin page with all the users in the system listed. They can always be disabled, but they may also be deleted. However they can only be deleted if they are not connected to anything critical. And I would like to not having to check that manually if it can be done easily in the database.
Note: I do not want to actually delete any user. I just want to display to the admin that a user could be deleted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试将其作为事务的一部分删除,如果成功则回滚事务。但是,我想接下来的问题是,为什么你一开始就不知道是否可以删除该行?
You could try deleting it as part of a transaction, and then roll back the transaction if it succeeds. BUT, I guess the immediate followup question is, why wouldn't you know in the first place if you could delete the row or not?
您可以使用视图来总结依赖项的数量,而不必担心存储数据和数据。保持最新状态。当依赖项数量为零时,使删除选项在 UI 中可用...
You could use a view to sum up the number of dependencies without having to worry about storing the data & keeping it current. When the number of dependencies is zero, make the delete option available in the UI...
您可以通过左连接到它们所连接的表来获取所有孤立行,例如,这将为您提供所有没有任何作业的用户 ID。
You can get all orphaned rows by left joining to the table they're connected to, e.g. this will give you all the user id's that don't have any jobs.
尝试这里的答案之一。
MySQL:如何我找到所有具有引用特定 table.column 的外键并且具有这些外键值的表?
Try one of the answers here.
MySQL: How to I find all tables that have foreign keys that reference particular table.column AND have values for those foreign keys?