Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:37:46+00:00 2026-06-10T14:37:46+00:00

I have an SQL Server 2012 db with a foreign key constraint that references

  • 0

I have an SQL Server 2012 db with a foreign key constraint that references Countries.CountryID = States.CountryID

I needed to recreate Countries table so I DROP this FOREIGN KEY from the States table (otherwise it wouldn’t allow me to DROP countries)

After I do that, I want to recreate the FK on States but it does not allow me to unless I specify NO CHECK as such:

ALTER TABLE States 
    WITH NOCHECK
    ADD CONSTRAINT FK_StatesCountries FOREIGN KEY (CountryID) REFERENCES Countries(CountryID) 
GO

I didn’t realize at first that some of the rows of States had CountryID that didnt have a matching Countries.CountryID record. Obviously WITH NOCHECK allowed me to proceed without error.

Now SQLServer with mark this FK as “not trusted”, MSDN says to re-enable query optimizer by checking all constraints. So why is it that the following line does NOT give me an error even though some States have an invalid CountryID?

 ALTER TABLE States
      CHECK CONSTRAINT ALL
 GO

I would think this should have raised an error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:37:47+00:00Added an answer on June 10, 2026 at 2:37 pm

    To make the constraints trusted again, you’d need to use this syntax, which admittedly looks a bit strange. The WITH CHECK is what causes the validation of existing data.

     ALTER TABLE States
          WITH CHECK CHECK CONSTRAINT ALL
     GO
    

    As before, this will throw an error until you resolve the States with bad CountryID values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2012 Express. I have a table which represents key/value pairs. keycol
I have a table in SQL Server 2012 that contains patch notes for different
I have a SQL Server 2008 table with a DateTime column that I'd like
I have a table stored on a SQL Server 2008, that associate a value
We have an interesting table query (SQL Server 2008) that fails with a different
I am using SQL Server 2012 I have a table called AMOUNTS and a
I have a datetime field in a SQL Server 2005 table that has values
I have installed the following components of SQL Server 2012: management tools - complete
We have an issue upgrading to SQL Server 2012. I am using the following
I have SQL Server 2008 with a table called ProductCategories designed like this: Id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.