2

I am in a tough situation, and looking for advice on how to proceed. I have to import some data from one environment to another. 80% of the records have been modified, so the plan is to simply delete all the existing records, and import the new ones.

Unfortunately this is a base table. Several other tables have FK's defined, pointing at this table. In order to delete the data, I need to either drop the FK's and recreate them when finished the import, or disable the constraints and re-enable them.

No records have been deleted. A copy of prod was made, and developers have tweaked the data and added new records.

Which approach would you take?

4
  • 1
    Wouldn't merge be an option here? Commented May 18, 2012 at 15:24
  • Yes, but in this case the version of SQL server is SQL-2000. I fixed the tag.
    – datagod
    Commented May 18, 2012 at 17:12
  • 1
    Take a look at can-foreign-key-constraints-be-temporarily-disabled-using-t-sql. Commented May 18, 2012 at 18:45
  • 3
    Ok. Then I guess the alternative is a update with a join followed by a insert with a where not exist. I would not drop or disable the FK's. I would use them to prevent me from messing up. Commented May 18, 2012 at 19:17

1 Answer 1

1

As recommended by Mikael Eriksson, I'd do the import without dropping/disabling the Foreign Keys.

The downside to this is if you want to delete any records who's values are referenced by an FK or want to update values such that the original value is no longer in the table and referenced by an FK.
In these cases you'd need to keep track of any such issues and decide how to handle each "conflict" (don't update/delete, alter the data in the other table or some other solution).

This will be quicker, easier and ensure data integrity compared to dropping/disabling the FKs, importing and then restoring the FKs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.