Created
January 14, 2021 13:55
-
-
Save lucaslgr/529351a197034ada05a116546ac4ef67 to your computer and use it in GitHub Desktop.
Script SQL para retornar todas foreign keys da respectiva database em uso
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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