Foreign Key issues

  • 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

  • Go to your source database to generate;

    Excerpt your desired snippet of script;

    Run it on your target database.

  • Hi,

    The best thing is export the source tables with all the grants,Primary key,Foreign Keys and Indexes.

    Or

    use the below syntax

    Ex:

    ALTER TABLE ORDERS

    ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID);

    Thanks -- Vj

    http://dotnetvj.blogspot.com

  • 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]

  • Oops, let's try this again:

    ALTER TABLE your_table WITH CHECK CHECK CONSTRAINT your_constraint;

    USE your_db;

    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

    __________________________________________________________________________________
    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]

  • Maybe this script can help you a bit...

    http://www.sqlservercentral.com/scripts/Miscellaneous/31808/

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply