Who’s got a foreign key on this table?

The need to identify dependencies in a database comes up fairly often at my day job.


I need to alter a table schema for whatever reason by adding, removing, or redefining columns. Or maybe it’s a reference table and I need to add, remove, or reorder records. Either way, messing with it could break things if other tables depend on it.

My first step, then, is to determine which tables depend on the table I mean to tamper with. By which I mean, which tables have foreign key relationships with the table in question.

Figuring this out is always a pain in the ass, because I keep forgetting that Microsoft SQL Server provides a stored procedure for this very purpose. You run it like so:

EXEC fp_keys 'table_name'

While this comes up fairly often, it doesn’t come up often enough to burn the syntax into my memory, thus this entry in my grimoire. Of course, I have no business visiting my own website from a work laptop, but I can always pull it up on my personal phone. That’ll save me a visit to Stack Overflow via Google.

Of course, were I sensible I might instead pick up a pen and write it down in a notebook, too. It’s not like I don’t have nice pens, with nice Japanese ink; I’ve even got a bottle of purple ink named after the lady who wrote Genji Monogatari.

(Were I truly sensible I would not have mistaken software development for a day job suitable for writers.)

One caveat: I wouldn’t count on this being available on Oracle, MySql, MariaDB, PostgreSQL, etc. This is probably one of those proprietary goodies.