Link to home
Start Free TrialLog in
Avatar of ccleebelt
ccleebeltFlag for United States of America

asked on

NoCheck Constraint

This is more a question to make sure I am doing this right.

I have a table, let's call it "clicks"

click_id int PK
product_id int  

I am trying to correct an error from a previous time where the product_id was set to default to zero.  I have a table of "products" and need to set the product_id to a null default in "clicks", remove the default constraint of 0 from "clicks" and then foreign key product_id in "clicks" to product_id in "products".

All is well with removing the constraint.  However, I have 50,000,000 rows of "clicks" historical data with a 0 in the product_id.  The job of sanitizing this data to update all the 0's to null is too big for now.  

Question - If I create the FK with a NOCHECK Constraint would I have the correct FK structure going forward and have the DB basically ignore the legacy data (for now)?

Later, I can run the scripts to fix the legacy data and then alter the FK to be correct and check the constraint.

Am I on the right path?
ASKER CERTIFIED SOLUTION
Avatar of QuinnDex
QuinnDex

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ccleebelt

ASKER

So basically, if I put the WITH NOCHECK ADD CONSTRAINT in the alter script when creating the FK the relationship it is bound going forward, but legacy data would be ignored then eh?

In other words, I get the FK between the tables for data from this point forward, but the legacy data would just not perform as well?
SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yep, the plan is to get this going and then once all the product_ids are set to null (in batches) run the ALTER TABLE clicks WITH CHECK CHECK CONSTRAINT ALL so it is trusted.