Skip to content

Missing foreign keys

MichelleTaylorRG edited this page Jun 19, 2019 · 1 revision

From http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/missing-foreign-key-constraints:

References are at the heart of a database. It is possible to create a beautiful database with perfectly working front end code that always, 100% of the time, does the right thing with your data. But, writing code is hard. Very hard! Your data is often the most important asset you own. You need to protect it with every bit of technology you can find. At the heart of protecting your data is referential integrity. What does this mean? It means that you shouldn’t be missing data, ever!

The test will check for columns that have ID in the name of the column where that column is not part of a primary key or foreign key constraint. Often times, this represents a missing constraint, but not always.

How to correct it: Correcting this problem seems simple at first. Just declare your foreign keys, right? Well, it’s not so simple. You see, there could be code running that deletes all the necessary data from the related tables. If you have code that deletes data in related tables in the wrong order, you will get referential constraint errors. Similar problems can occur with updates and inserts. The order in which you do things is important when you have referential constraints.

Clone this wiki locally