• Disco (1/14/2008)


    I moved two tables from one server to another... after moving, the foreign key relationship is missing on one of the columns in the table in the destination server..does anybody have any idea as to how to add the FK to that column so that the relationship is maintained properly..

    Thanks

    If, while the FK constraint was absent, there have been modifications to the table(s) involved, addition of the constraint will give you an error if there are any data integrity violations.

    In any case, it wouldn't hurt to make sure the added constraint is "trusted":

    ALTER TABLE ;

    Make sure there are no other non-trusted constraints in your database:

    USE ;

    SELECT OBJECT_NAME(parent_object_id) AS table_name, name

    FROM sys.check_constraints

    WHERE is_not_trusted = 1

    UNION ALL

    SELECT OBJECT_NAME(parent_object_id) AS table_name, name

    FROM sys.foreign_keys

    WHERE is_not_trusted = 1

    ORDER BY table_name;

    Basically, a constraint can be in one of the following stages:

    - disabled: not enforcing data integrity

    - enabled: enforcing data integrity on new data

    - trusted: enforcing data integrity on new and existing data

    The optimizer makes use of this info to construct efficient query plans.

    See, for example,

    http://sqlblog.com/blogs/tibor_karaszi/archive/2008/01/12/non-trusted-constraints-and-performance.aspx

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]