top of page

WITH NOCHECK

If you create a table like this:

CREATE TABLE Examples.BadData ( PositiveValue int NOT NULL );

INSERT INTO Examples.BadData(PositiveValue) VALUES (-1),(-2),(-3),(-4);

Then, you want to use CHECK CONSTRAINT,

ALTER TABLE Examples.BadData ADD CONSTRAINT CHKBadData_PostiveValue CHECK(PositiveValue > 0);

In this case, you can use create the constraint and leave the bad data. This can be done by specifying WITH NOCHECK which bypasses the data check:

ALTER TABLE Examples.BadData WITH NOCHECK ADD CONSTRAINT CHKBadData_PostiveValue CHECK(PositiveValue > 0);

After you fixed data you can do as follows,

ALTER TABLE Examples.BadData WITH CHECK

CHECK CONSTRAINT CHKBadData_PostiveValue;

You can check whether the constraints are disabled or trusted using the follow T-SQL,

SELECT is_not_trusted, is_disabled FROM sys.check_constraints WHERE OBJECT_SCHEMA_NAME(object_id) = 'Examples' and OBJECT_NAME(object_id) = 'CHKBadData_PostiveValue';

 

You can also use WITH NOCHECK for Foreign Constraint. Here is an example,

You must modify the ProductReview Table to meet the following requirements:

✑ The table must reference the ProductID column in the Product table ✑ Existing records in the ProductReview table must not be validated with the Product table. ✑ Deleting records in the Product table must not be allowed if records are referenced by the ProductReview table. ✑ Changes to records in the Product table must propagate to the ProductReview table.

To enable referential integrity for the ProductReview table, you can do as follows,

After table dbo.ProductReview

With NOCHECK

ADD CONSTRAINT FK_ProductReview_Product Foreign Key (ProductID)

Reference Product (ProductID)

On Delete NO ACTION

ON UPDATE CASCADE

aboutME

I am John Fan Zhang, a data analyst and finance researcher. I hold a PhD in finance, CFA charter and full membership of CFA New Zealand Society. I have fifteen-year experience in corporate investment and eight-year experience in advanced data analysis. My research focuses on the effect of social psychology (culture) on financial decisions. Finance research involves heaps of data analyses that lead me to the data field. I am a Microsoft Certified Solutions Expert (MCSE): Data Management and Analytics (Excel, Power BI, and SQL). Aside from Excel, Power BI and SQL, I am also familiar with econometric tools such as Stata, Eviews, and MATLAB. I use OX and Python for programming. I am an active data community event participant, volunteer, speaker, moderator, program reviewer, including PASS Marathon 2020, Global AI BootCamp Auckland 2019, SQL Saturday Auckland (2017, 2018, 2019), and Definity Conference (2018, 2019, 2020, Auckland, New Zealand).

Auckland, New Zealand

  • Google Site
  • Twitter
  • LinkedIn

©2016 BY JOHN'S DATA STORY

bottom of page