Skip to content

Instantly share code, notes, and snippets.

@mark-d-holmberg
Created March 23, 2020 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark-d-holmberg/0ea4f1db8afa9dc87dd82a0bfdeb4a87 to your computer and use it in GitHub Desktop.
Save mark-d-holmberg/0ea4f1db8afa9dc87dd82a0bfdeb4a87 to your computer and use it in GitHub Desktop.
Show foreign keys in a maria database.
select concat(fks.constraint_schema, '.', fks.table_name) as foreign_table,
'->' as rel,
concat(fks.unique_constraint_schema, '.', fks.referenced_table_name)
as primary_table,
fks.constraint_name,
group_concat(kcu.column_name
order by position_in_unique_constraint separator ', ')
as fk_columns
from information_schema.referential_constraints fks
join information_schema.key_column_usage kcu
on fks.constraint_schema = kcu.table_schema
and fks.table_name = kcu.table_name
and fks.constraint_name = kcu.constraint_name
-- where fks.constraint_schema = 'database name'
group by fks.constraint_schema,
fks.table_name,
fks.unique_constraint_schema,
fks.referenced_table_name,
fks.constraint_name
order by fks.constraint_schema,
fks.table_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment