Sql – Indirect Foreign Key

You should always enforce referential integrity by using “normal” FOREIGN KEYs.

In a nutshell, FOREIGN KEYs have the following advantages:

  1. They are already implemented within the DBMS.
  2. They are declarative, self-documenting and “obvious”.
  3. They cannot be bypassed (unless explicitly disabled or dropped).
  4. They are correct.
  5. They are fast.
  6. They support cascading referential actions (such as ON DELETE CASCADE).
  7. The DBMS knows the data is related, allowing it to find a better query plan in some cases .
  8. If you are using an ORM tool, it can automatically generate references between objects.

And here are the corresponding disadvantages of enforcing referential integrity in the application code:

  1. You are duplicating the work that has already been done.
  2. It’s imperative, probably “buried” deep in your application source code, and harder to maintain.
  3. A single client application that has a bug can break the referential integrity (and corrupt the data).
  4. You are likely to implement them incorrectly in your application code. It looks simple from the outset, but in a concurrent environment, it is easy to introduce race conditions.
  5. Even if you have implemented them correctly, you probably used some form of locking to avoid race conditions, which is likely to be slower / less scalable than specially optimized FKs built into the DBMS.
  6. You have to implement cascading yourself.
  7. The DBMS doesn’t know the data is related, which may produce sub-optimal query plan.
  8. You may need to do more manual work in your ORM tool of choice.

Is there a name for this?

Not that I know of. I heard a term “generic FKs” being used, but that’s probably not universal.

Is it good practice?

No (see above).

Any performance considerations?

Yes (see above).

Leave a Comment