Which Tables Reference This Table? Finding Incoming Foreign Keys in PostgreSQL, MySQL, SQL Server and SQLite
Disclosure: I build Schemity , a desktop ERD tool - this post is from our blog and uses it for the examples. TL;DR: Ask the catalogue for foreign keys whose referenced table is yours: pg_constraint.confrelid in PostgreSQL, REFERENCED_TABLE_NAME in MySQL, sys.foreign_keys.referenced_object_id in SQL Server, and pragma_foreign_key_list across every table in SQLite. Then check two things the list…
To discover which tables reference a specific table, you need to query the database's catalog for foreign keys whose target table matches the table you're interested in. Each database management system (DBMS) stores this information differently, but they all provide a way to perform this lookup. In SQLite, since it stores foreign keys only in the CREATE TABLE statement of the table declaring them, you must scan every table's list to find incoming foreign keys.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to