Solved

NoCheck Constraint

Posted on 2013-12-11
4
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
QuinnDex earned 250 total points
ID: 39712077
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
ID: 39712111
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:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 39712529
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
ID: 39712733
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

710 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