Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

NoCheck Constraint

Posted on 2013-12-11
4
Medium Priority
?
279 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 1000 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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 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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

715 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