Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Detection of Broken Forign Key Relationship

By vrata, 2013/05/21

Under special circumstances, it could happen that database contains "orphan" records. Data in dependant table exists without it's "parent" record in a master table. It could be caused by a bulk insert operation without CHECK_CONSTRAINTS option, or by disabling foreign key for a while or adding a foreign key to a table that already contains data without checking existing data for validity.

In SQL2000, there is a function DBCC CHECKCONSTRAINTS, that checks all tables (or just a chosen one) for consistency with all (or chosen) constraints. (actually, there was a bug in version 8.00.194 that didn't allow to run this statement against database with special character eg. space in a table name eg. Northwind database :-)).

As far as I know there is no similar functionality in MS SQL7.

If you would like to check your data, you can try following SP. It has one optional parameter for a name of a table. If you don't specify the name, all the user tables in a current database will be checked. The output format is similar to DBCC CHECKCONSTRAINTS.

If you need to check just broken FOREIGN KEY relationship, enclosed stored procedure runs faster than DBCC.

Total article views: 156 | Views in the last 30 days: 3
 
Related Articles
ARTICLE

A Check and Foreign Key Constraint Improves Query Performance

Check and foreign key constraint do not degrade performance but actually improve it.

FORUM

Bitwise operations for Check or Foreign Key Constraint

Need help with logic for foreign key or check constraint for bitwise operations

ARTICLE

Foreign Keys

Foreign keys are an important part of a relational database. New author Ravi Lobo takes a look at fo...

FORUM

Primary-Foreign Key

Primary-Foreign Key

SCRIPT

Generate index on foreign key columns

Script find all foreign key without index in database and create new one for them.

Tags
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones