开发者

MySQL: How to find where a specific primary key is used as a foreign key in other tables?

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 m开发者_运维百科e, on entry of the primary key (column name, not actual value), if that key is used as a foreign key somewhere else.

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


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


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

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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜