WITH CHECK | NOCHECK OPTION FOR EXISTING DATA CHECK ENFORCEMENT IS IGNORED

Robert MacLean picture Robert MacLean · Jan 28, 2013 · Viewed 8.7k times · Source

I am using Visual Studio 2012, with a SQL database project and running database analysis against this database and it is raising the following warning: WITH CHECK | NOCHECK OPTION FOR EXISTING DATA CHECK ENFORCEMENT IS IGNORED.

My understanding of the error is that the CHECK and NOCHECK constraints will be ignored for the existing data when the script is run (in each case for me, it is an ALTER TABLE).

My question is thus, why would a check constraint be ignored?

Warning ID number

It seems the warning ID has changed so I am including both below so that it is easily searchable in future.
In Visual Studio 2010 this had the warning ID of: SQL03159
In Visual Studio 2012 this has the warning ID of: SQL70588

Related Information

ALTER TABLE [dbo].[MyTable] CHECK CONSTRAINT [FK_MyTable_SomeCol]

Answer

Sam picture Sam · Nov 12, 2013

In my case, this occurred in a script I imported that had a structure like this:

CREATE TABLE [dbo].[ELMAH_Error]
(
    [ErrorId]     UNIQUEIDENTIFIER NOT NULL,
) 
GO
ALTER TABLE [dbo].[ELMAH_Error] WITH NOCHECK ADD 
    CONSTRAINT [PK_ELMAH_Error] PRIMARY KEY NONCLUSTERED ([ErrorId]) ON [PRIMARY]
GO

When I look at the above code, the WITH NOCHECK looks redundant since the table should have only just been created and therefore be empty. So I suspect this code analysis warning is pointing out this redundancy.