Skip to content

December 20, 2011

Finding Untrusted Foreign Key Constraints

by Scott Newman

To find untrusted foreign key constraints, use this:

SELECT 
name AS ForeignKey,
OBJECT_NAME(parent_object_id) AS TableName,
OBJECT_NAME(referenced_object_id) AS ReferencedTable,
is_not_trusted
FROM sys.foreign_keys

In order to make these constraints be re-trusted, you have to re-check the constraint, so don’t run this on a production system during the day on large tables.

ALTER TABLE TableName WITH CHECK CHECK CONSTRAINT ConstraintName;  

The key here is the WITH CHECK.  If you omit that the query will still execute successfully, but your constraint still will not be trusted afterwards.

Read more from SQL Server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: