Skip to content

Instantly share code, notes, and snippets.

@favrik
Created July 12, 2017 14:45
Show Gist options
  • Save favrik/a78b666b1c063e3ba84e02a287ef27a6 to your computer and use it in GitHub Desktop.
Save favrik/a78b666b1c063e3ba84e02a287ef27a6 to your computer and use it in GitHub Desktop.
SELECT
`TABLE_SCHEMA`, -- Foreign key schema
`TABLE_NAME`, -- Foreign key table
`COLUMN_NAME`, -- Foreign key column
`REFERENCED_TABLE_SCHEMA`, -- Origin key schema
`REFERENCED_TABLE_NAME`, -- Origin key table
`REFERENCED_COLUMN_NAME` -- Origin key column
FROM
`INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` -- Will fail if user don't have privilege
WHERE
`TABLE_SCHEMA` = SCHEMA() -- Detect current schema in USE
AND `REFERENCED_TABLE_NAME` IS NOT NULL; -- Only tables with foreign keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment