Skip to content

Instantly share code, notes, and snippets.

@mikehins
Created February 2, 2024 14:23
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 mikehins/963183b2833ed0b90a4f8e07aebefc9f to your computer and use it in GitHub Desktop.
Save mikehins/963183b2833ed0b90a4f8e07aebefc9f to your computer and use it in GitHub Desktop.
# https://soft-builder.com/how-to-list-all-foreign-keys-in-mysql-database/
SELECT RefCons.constraint_schema, RefCons.table_name, RefCons.referenced_table_name, RefCons.constraint_name, KeyCol.column_name
FROM information_schema.referential_constraints RefCons
JOIN information_schema.key_column_usage KeyCol ON RefCons.constraint_schema = KeyCol.table_schema
AND RefCons.table_name = KeyCol.table_name
AND RefCons.constraint_name = KeyCol.constraint_name
WHERE RefCons.constraint_schema = 'DATABASE_NAME';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment