Menu

1 / 2

Which Tables Reference This Table? Finding Incoming Foreign Keys in PostgreSQL, MySQL, SQL Server and SQLite

DEV Community: mysql·Son Tran·6 days ago
#vltzA9GH
#devtomysql#devto#dev#table#keys
Long article (1838 words) — no AI summary yet.
Reading 0:00
15s threshold

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 does not tell you: whether a key appears once per partition, and whether it still enforces anything, because disabled and unvalidated keys are listed like the rest.

To find the tables that reference a table, ask the database's catalogue for foreign keys whose referenced table is yours. Every engine records that, but each stores it differently, and the answer it gives back has two traps: some engines list one foreign key several times, and most engines can list keys that enforce nothing.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.