Solved

NoCheck Constraint

Posted on 2013-12-11
4
263 Views
Last Modified: 2013-12-11
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?
0
Comment
Question by:ccleebelt
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
QuinnDex earned 250 total points
Comment Utility
a defualt constraint works on newly added or up dated data, existing data will not be checked by it unles you up date it, in which case you can add a trigger to change the 0 to a null value thus reducing the update task ahead
0
 

Author Comment

by:ccleebelt
Comment Utility
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?
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 250 total points
Comment Utility
Yes, that's basically it: if you add the constraint with NOCHECK, then until/unless you update the product_id column in the "clicks" table, it won't be checked.  But any newly inserted row will be.

Rather than a trigger, I'd just start UPDATEing "clicks" rows in batches, setting any product_id = 0 to NULL.  A NULL value in a column will cause the fk check to be ignored/bypassed.
0
 

Author Comment

by:ccleebelt
Comment Utility
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.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now