11

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.

4

4 回答 4

4

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

于 2010-09-14T18:42:02.407 回答
1

您可以使用视图来总结依赖项的数量,而不必担心存储数据并使其保持最新。当依赖项的数量为零时,使 UI 中的删除选项可用...

于 2010-09-14T18:42:07.867 回答
1

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

SELECT u.id FROM users u LEFT JOIN jobs j on u.id=j.user_id WHERE j.user_id is null;
于 2010-09-14T19:11:02.803 回答
0

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

于 2010-09-14T18:42:48.527 回答