Skip to content

Instantly share code, notes, and snippets.

@lucaslgr
Created January 14, 2021 13:55
Show Gist options
  • Save lucaslgr/529351a197034ada05a116546ac4ef67 to your computer and use it in GitHub Desktop.
Save lucaslgr/529351a197034ada05a116546ac4ef67 to your computer and use it in GitHub Desktop.
Script SQL para retornar todas foreign keys da respectiva database em uso
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