检查MySQL中是否可以删除一行

发布于 2024-09-19 04:41:04 字数 226 浏览 3 评论 0原文

有没有办法检查一行是否可以被删除?例如,它当前没有通过受限制的外键连接到其他任何东西。

原因:我正在制作一个管理页面,其中列出了系统中的所有用户。它们始终可以被禁用,但也可以被删除。然而,只有当它们没有连接到任何关键的东西时才可以被删除。如果可以在数据库中轻松完成,我希望不必手动检查。

注意:我不想实际删除任何用户。我只想向管理员显示用户可以被删除。

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 技术交流群。

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

发布评论

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

评论(4

梦归所梦 2024-09-26 04:41:04

您可以尝试将其作为事务的一部分删除,如果成功则回滚事务。但是,我想接下来的问题是,为什么你一开始就不知道是否可以删除该行?

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?

风吹过旳痕迹 2024-09-26 04:41:04

您可以使用视图来总结依赖项的数量,而不必担心存储数据和数据。保持最新状态。当依赖项数量为零时,使删除选项在 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...

第七度阳光i 2024-09-26 04:41:04

您可以通过左连接到它们所连接的表来获取所有孤立行,例如,这将为您提供所有没有任何作业的用户 ID。

SELECT u.id FROM users u LEFT JOIN jobs j on u.id=j.user_id WHERE j.user_id is null;

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.

SELECT u.id FROM users u LEFT JOIN jobs j on u.id=j.user_id WHERE j.user_id is null;
稚气少女 2024-09-26 04:41:04

尝试这里的答案之一。
MySQL:如何我找到所有具有引用特定 table.column 的外键并且具有这些外键值的表?

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