Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« Testing backup compression in SQL Server 2008Procedures with cursors »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

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 code below 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. The code presented below exists to highlight potential problems. You must still determine if this potential problem is real, and then act accordingly.

How to detect this problem:

  1. SELECT  C.Table_Name, C.Column_Name
  2. FROM    Information_Schema.Columns C
  3.         INNER Join Information_Schema.Tables T
  4.           ON C.Table_Name = T.Table_Name
  5.           And T.Table_Type = 'Base Table'
  6.         LEFT Join Information_Schema.constraint_column_usage U
  7.           ON C.Table_Name = U.Table_Name
  8.           And C.Column_Name = U.Column_Name
  9. WHERE   U.Column_Name IS NULL
  10.         And C.Column_Name Like '%id%'
  11. ORDER BY C.Table_Name, C.Column_Name

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.

Level of severity: High

Level of difficulty: High

About the Author

George has been developing software professionally for 19 years, first for the department of defense, and then for various other companies. In 1998, George started his software company, Orbit Software, specializing in School Bus Transportation software. His specialty is refining SQL Server queries to deliver optimal performance.
Social SitingsTwitterLTD RSS Feed
675 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

2 comments

Comment from: John Bates [Visitor] · http://codehq.net/
Thanks George, works a treat. I made a couple of mods that may be useful to someone:

(1) Removed the trailing percent sign in the LIKE statement so only column names that end with "ID" are checked;

(2) Added a COLLATE clause for case-matching, to avoid columns like "AmountPaid".

So the column name comparison becomes:

(C.Column_Name COLLATE Latin1_General_BIN LIKE '%ID')

HTH.
12/06/09 @ 20:46
Comment from: Michael J Swart [Visitor] · http://michaeljswart.com
Wow, I came up with something extremely similar and posted before finding this page. I've linked from my blog to this page. Hope you don't mind:
http://michaeljswart.com/?p=320
12/07/09 @ 12:59

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)