Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database Design Without Foreign Keys

After having worked at various employers I've noticed a trend of "bad" database design with some of these companies - primarily the exclusion of Foreign Keys Constraints. It has always bugged me that these transactional systems didn't have FK's, which would've promoted referential integrity.

  • Are there any scenarios, in transactional systems, whereby the omission of FK's would be beneficial?

  • Has anyone else experienced this, if so what was the outcome?

  • What should one do if they're presented with this scenario and their asked to maintain/enhance the system?

like image 469
culturalanomoly Avatar asked Aug 22 '12 02:08

culturalanomoly


People also ask

Can we do data design without foreign key?

You don't have to configure a foreign key constraint on a column just because it refers to another column. You could instead configure two tables such that one refers to the other, but without any defined foreign key.

Does every database need a foreign key?

Note that foreign keys are not mandatory, and a table may have no foreign keys. Conversely, every column in a table may have a foreign key constraint.

Can a relational database have no foreign keys?

There is no reason not to use Foreign Key Constraints, and to use a relational database.

When should you not use foreign keys?

There's one good reason not to use them: If you don't understand their role or how to use them. In the wrong situations, foreign key constraints can lead to waterfall replication of accidents. If somebody removes the wrong record, undoing it can become a mammoth task.


2 Answers

I cannot think of any scenario where, if two columns have a dependency, they should not have a FK constraint set up between them. Removing referential integrity may certainly speed up database operations but there's a pretty high cost to pay for that.

I have experienced such systems and the usual outcome is corrupted data, in the sense that records exists that shouldn't exist (or vice versa). These are the sort of systems where people believe they're okay because the application takes care of it, not caring that:

  • Every application has to take care of it, rather than one DB server.
  • It only takes one bug, or malignant app, to screw it up for everyone.
  • It is the responsibility of the database to protect itself! That is one of its best features.

As to what you should do, I simply put forward the possible things that can go wrong and how using FKs will prevent that (often with a cost/benefit analysis "skewed" toward my viewpoint, if necessary). Then let the company decide - it is their database, after all.

like image 154
paxdiablo Avatar answered Oct 01 '22 03:10

paxdiablo


There is a school of thought that a well-written application does not need referential integrity. If the application does things right, the thinking goes, there's no need for constraints.

Such thinking is akin to not doing defensive programming because if you write the code correctly, you won't have bugs. While true, it simply won't happen. Not using appropriate constraints is asking for data corruption.

As for what you should do, you should encourage the company to add constraints at every opportunity. You don't want to push it to the point of getting in trouble or making a bad name for yourself, but as long as the environment is appropriate, keep pushing for it. Everyone's life will be better in the long run.

like image 38
wadesworld Avatar answered Oct 01 '22 01:10

wadesworld