Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hugo53/c5be02962ec4df2224d9be90dbfec6c4 to your computer and use it in GitHub Desktop.
Save hugo53/c5be02962ec4df2224d9be90dbfec6c4 to your computer and use it in GitHub Desktop.
MySQL - Get list of tables reference to a table with foreign key
SELECT
    TABLE_SCHEMA AS 'Database',
    TABLE_NAME AS t1,
    REFERENCED_TABLE_NAME AS 't2 (reference table)',
    COLUMN_NAME AS 't1 column',
    REFERENCED_COLUMN_NAME AS 't2 column (reference table)',
    CONSTRAINT_NAME AS 't1 (constrain name)'
FROM
    information_schema.key_column_usage
WHERE
    referenced_table_name IS NOT NULL 
    AND TABLE_SCHEMA = 'database_name'
    AND REFERENCED_TABLE_NAME = 'table_name'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment