MySQL:如何找到特定主键在其他表中用作外键的位置?

时间:2022-10-12 09:03:19

I'm working on implementing a function to prevent removal of certain elements in a database (through the front end) if they have other items associated with them in other tables. Otherwise those other tables are looking for keys that aren't there.

我正在努力实现一个函数,以防止删除数据库中的某些元素(通过前端),如果他们在其他表中有其他与之关联的项目。否则那些其他表正在寻找不存在的键。

If you understood that my hat is off to you.

如果你明白我的帽子是给你的。

I have many sets of tables to look through and need either a SQL query or a MySQL Workbench feature that can tell me, on entry of the primary key (column name, not actual value), if that key is used as a foreign key somewhere else.

我有很多表要查看,需要SQL查询或MySQL Workbench功能,可以告诉我,在输入主键(列名,而不是实际值)时,如果该键在某处用作外键其他。

Otherwise if anyone knows an offhand workaround, that would be great too!

否则,如果有人知道一个随便的解决方法,那也会很棒!

2 个解决方案

#1


5  

SELECT 
  table_name, column_name     
FROM
  information_schema.key_column_usage
WHERE
  referenced_table_name = '<table>'
  and referenced_column_name = '<primary key column>'

#2


0  

A solution is described in this post to retrieve this information from information_Schema table.

本文描述了一种解决方案,用于从information_Schema表中检索此信息。

1) If you want to work on these tables from your code, then fetch them as a container, for example ArrayList in your code and perform your logic.

1)如果您想从代码中处理这些表,那么将它们作为容器获取,例如代码中的ArrayList并执行您的逻辑。

2) If you want to work on these tables from your Stored Procedure, then use temporary tables to achive the same work you'd do in your java code through containers.

2)如果您想从存储过程中处理这些表,那么使用临时表来实现您通过容器在Java代码中执行的相同工作。

#1


5  

SELECT 
  table_name, column_name     
FROM
  information_schema.key_column_usage
WHERE
  referenced_table_name = '<table>'
  and referenced_column_name = '<primary key column>'

#2


0  

A solution is described in this post to retrieve this information from information_Schema table.

本文描述了一种解决方案,用于从information_Schema表中检索此信息。

1) If you want to work on these tables from your code, then fetch them as a container, for example ArrayList in your code and perform your logic.

1)如果您想从代码中处理这些表,那么将它们作为容器获取,例如代码中的ArrayList并执行您的逻辑。

2) If you want to work on these tables from your Stored Procedure, then use temporary tables to achive the same work you'd do in your java code through containers.

2)如果您想从存储过程中处理这些表,那么使用临时表来实现您通过容器在Java代码中执行的相同工作。