Skip to content

Instantly share code, notes, and snippets.

@mystikraz
Created September 21, 2021 05:09
Show Gist options
  • Select an option

  • Save mystikraz/ee15cf81ad04cb122abc99f034b83342 to your computer and use it in GitHub Desktop.

Select an option

Save mystikraz/ee15cf81ad04cb122abc99f034b83342 to your computer and use it in GitHub Desktop.
list all references of a table in Mysql
SELECT
ku.CONSTRAINT_NAME AS "Foreign key",
-- CONCAT("`", ku.TABLE_SCHEMA, "`.`", ku.TABLE_NAME, "`") AS "In",
ku.TABLE_NAME AS "In",
GROUP_CONCAT(ku.COLUMN_NAME) AS "Source column",
CONCAT("`", ku.REFERENCED_TABLE_SCHEMA, "`.`", ku.REFERENCED_TABLE_NAME, "`") AS "References",
GROUP_CONCAT(ku.REFERENCED_COLUMN_NAME) AS "Target column"
FROM information_schema.KEY_COLUMN_USAGE AS ku
WHERE ku.REFERENCED_TABLE_SCHEMA = 'dev.bidenergy.com'
AND ku.REFERENCED_TABLE_NAME = 'site'
GROUP BY ku.CONSTRAINT_NAME
HAVING `In` != `References`
select table_name
from information_schema.KEY_COLUMN_USAGE
where table_schema = 'dev.bidenergy.com'
and referenced_table_name = 'site';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment