Error adding Foreign Key

  • Over the weekend, I used BCP to move the data for one database from one server to another. On the destination server, I removed all of the indexes and contraints for speed purposes. When I try to re-apply the constraints to the destination server, I'm getting an error. What I found, based on the error code, is that the table/column being referenced does not contain one or more values in the table/column I'm trying to reapply the contraint to.

    Everything is identical on both servers and the data all matches too. So, I looked at the source server to see how the constraint was defined there and found an option called "Check Existing Data On Creation Or Re-enabling" set to No. I assume my predecessor had similar troubles.

    My question is, how do I modify my alter table command below with this option set to NO? I can't find it anywhere.

    ALTER TABLE [dbo].[SearchItemWord] ADD

    CONSTRAINT [FK_SearchItemWord_SearchItem] FOREIGN KEY

    (

    [SearchItemID]

    ) REFERENCES [dbo].[SearchItem] (

    [SearchItemID]

    ) ON DELETE CASCADE ON UPDATE CASCADE

  • Add the WITH NOCHECK clause.

    [font="Courier New"]ALTER TABLE [dbo].[SearchItemWord] WITH NOCHECK

    ADD CONSTRAINT [FK_SearchItemWord_SearchItem] FOREIGN KEY

    (

    [SearchItemID]

    )

    REFERENCES [dbo].[SearchItem]

    (

    [SearchItemID]

    ) ON DELETE CASCADE ON UPDATE CASCADE [/font]



    Scott Duncan

    MARCUS. Why dost thou laugh? It fits not with this hour.
    TITUS. Why, I have not another tear to shed;
    --Titus Andronicus, William Shakespeare


Viewing 2 posts - 1 through 1 (of 1 total)

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