Foreign Keys, for People Who Didn't Plan to Learn Them

RedHub AI Editorialupdated November 5, 20267 min read

Brass padlocks hang on a pegboard, each with its key below, except one lit red whose key hook is bare.
Jump to a section8

A foreign key is a rule on a column that says its value has to already exist as a real row somewhere else — for example, every order's user id must match an actual row in your accounts table. Without one, a column that looks exactly the same is just a value someone wrote down: correct today, and the database will accept a broken or made-up reference tomorrow without complaint.

TL;DR: Two columns can look identical — both named user_id, both holding what appears to be a real id — and behave completely differently. One is checked by the database every time it's written. The other is trusted on faith. A foreign key is what turns "probably correct" into "the database won't let it be wrong."

The bouncer versus the doorman who's trying to remember faces

Picture two ways of controlling who gets into a building. One has a list at the door, and every name gets checked against it before anyone's let in — if you're not on the list, you don't get through, no matter how convincing you sound. The other has a doorman who's told to remember the regulars and wave through anyone who looks right. Both can work, most nights. The difference only shows up on the night someone new and wrong-looking gets waved through anyway, because there was never a list — just a memory, and memories are the part that eventually gets it wrong.

A foreign key is the list at the door. A column that merely holds a copied value — an id typed in once, an email pasted from another screen — is the doorman going on memory. It usually works. When it fails, it fails quietly, and you won't know which write it was until something downstream breaks.

What a foreign key checks

Concretely: when a column has a foreign key pointing at another table, the database refuses to save a row where that column's value doesn't already exist over there. Try to save an order with a user id that doesn't correspond to any real account, and the database says no — not your application code, the database itself, on every write, regardless of which part of your app is doing the writing.

This is standard relational database behavior, not a niche technique tied to one product. Two things do vary from one database to the next. The syntax for setting it up differs, and not every engine enforces foreign keys by default, so confirm enforcement is switched on in the one you use. Check its documentation for the how. The guarantee itself, that the database refuses a reference that doesn't exist, is the stable part.

Column with a foreign keyColumn that's just a copied value
Looks like, in the table editorAn id, same as the other kindAn id, or an email, or a name — same as the other kind
What happens if the reference is wrongThe database refuses the writeThe database saves it anyway
What happens if the referenced row is deletedThe database enforces whatever you decided — block it, remove the dependent rows too, or clear the referenceNothing. The old value just sits there, pointing at nothing, until something tries to use it
Who's enforcing the ruleThe database, on every write, from any code pathWhatever code remembered to check — and only that code

Why the difference matters more than it looks like it should

The reason this is worth learning instead of skipping is that the failure mode is delayed and quiet, not immediate and loud. A copied-value column will look completely fine in every test you run while there's only one code path writing to it. The gap only opens once there's a second one — an admin panel, a migration, a bulk import script someone writes six months in — and that second path doesn't have to be careless to get it wrong. It just has to not know about the rule the first path was quietly enforcing by hand.

That's the honest way to describe a copied-value reference: not broken, fragile. It's doing the job right now. It's doing the job because exactly one piece of code remembers to check it, and the day a second piece of code writes to that table without knowing the rule, the reference can go bad and nothing will say so.

What deleting a referenced row does to the rows pointing at it

This is the part people find useful once they know it exists: a foreign key isn't just a check on writes, it's also a decision about what happens on deletes. If a user account gets deleted, what should happen to their orders? Most relational databases let you choose — block the deletion until the orders are dealt with, delete the orders along with the account, or clear the reference and leave the orders orphaned on purpose. None of these is automatically right; it depends on whether you want to keep historical records, and that's a decision worth making deliberately instead of defaulting into whatever your builder happened to generate.

A different problem this doesn't solve

If what you're checking is a CSV file you're about to import, instead of a live table already sitting in your database, that's a related but different question — whether the file's structure, encoding, and values are clean before they ever reach a table. That's the job of the CSV / Data-Export Schema & Quality Gate, not this guide. Foreign keys are about references between tables that already exist inside your database.

Checking your own tables

  1. Find every column that looks like a reference. Anything named like user_id, order_id, or similar, pointing at another table. Test: list them out, table by table.
  2. Check whether each one is a real foreign key or a copied value. Your table editor's schema view usually shows this directly. Test: look for the constraint definition — if it's not there, the database isn't checking the column, whatever its name suggests.
  3. Decide what should happen on delete, for the ones that matter. Don't leave it to whatever the default happened to be. Test: for each real foreign key, confirm the delete behavior matches what you'd want if that parent row disappeared.

A literal sentence for your AI builder: "Make the user id column on this table a real foreign key referencing the accounts table, not just a stored value, and set it to [block the delete / cascade / clear the reference] if the account is removed."

Scope: this explains a database mechanism. It isn't a review of your app's security posture and it doesn't tell you whether your data is currently safe — it tells you what a foreign key is and how to check whether you have one.

The Data Model Sanity Bench ($69) asks this question about one column per table: the owner column. Real reference, plain copied value, none, or can't find. A copied value reads YOU ENFORCE IT, because your code is the only thing keeping the owner real. If you can't tell whether the owner column is a real reference, the bench scores it as the worst case, no owner at all, until you go and look. It does not ask about your other reference columns, like the order id on a line item, and it does not ask about delete behavior. Those stay on the checklist above.

More in this guide

Frequently asked questions

What is a foreign key, in plain terms?

A rule on a column that forces its value to match a real row in another table. Without it, the database will happily save a reference to something that doesn't exist; with it, that write gets refused.

What happens if I delete a row that other rows refer to?

It depends on how the foreign key is set up. You can choose to block the deletion, delete the dependent rows too, or clear the reference on them. This is a decision worth making on purpose instead of accepting whatever default your schema shipped with.

Is a foreign key the same thing as an owner column?

Related but not identical. An owner column is the idea — a piece of data naming who a row belongs to. A foreign key is what makes that column enforced instead of just trusted. You can have an owner column that isn't a foreign key; it just means the database isn't checking it.

Do I need a foreign key on every relationship in my schema?

Most relationships between a records table and the table it depends on benefit from one, starting with the column that names a row's owner. A reference list you seeded yourself, like plan tiers, is usually the thing being pointed at instead of the thing doing the pointing, so it rarely needs a foreign key of its own.

Does this tell me if my database is secure?

No. This explains one mechanism — how references between tables are checked — and how to see whether you have it. It isn't a security review of your app.

How it decides
Diagram of the Data-Model Sanity Bench: ten tables, a prerequisite ownership gate, and a sample reading NOTHING TO BUILD ON.

The gate this post refers to, drawn from the tool’s own logic. See the tool.