Finding Untrusted Foreign Key Constraints

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.

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 )

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.