Answered by:
Re enable foreign key constraint - CHECK CONSTRAINT vs WITH CHECK CHECK CONSTRAINT

Question
-
I am enabling foreign key constraints after previously disabling them for a bulk data load. Regarding how to re-enable the foreign key constraints, I've seen examples with two different syntax (example below) but am not sure if the result is the same for both. Can anyone explain to me what does WITH CHECK CHECK CONSTRAINT do differently from CHECK CONSTRAINT?
/* REenable constraints */
ALTER TABLE [dbo].[myTable] CHECK CONSTRAINT cons_f_column1
ALTER TABLE [dbo].[myTable] WITH CHECK CHECK CONSTRAINT cons_f_column1In what situations would you use one over the other?
Thursday, February 10, 2011 2:36 AM
Answers
-
Hi MGS,
As per Books On Line:
WITH CHECK | WITH NOCHECK
Specifies whether the data in the table is or is not validated against a newly added or re-enabled FOREIGN KEY or CHECK constraint. If not specified, WITH CHECK is assumed for new constraints, and WITH NOCHECK is assumed for re-enabled constraints.
If you do not want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. We do not recommend doing this, except in rare cases. The new constraint will be evaluated in all later data updates. Any constraint violations that are suppressed by WITH NOCHECK when the constraint is added may cause future updates to fail if they update rows with data that does not comply with the constraint.
The query optimizer does not consider constraints that are defined WITH NOCHECK. Such constraints are ignored until they are re-enabled by using ALTER TABLE table CHECK CONSTRAINT ALL.
CHECK CONSTRAINTS
CHECK constraints enforce domain integrity by limiting the values that are accepted by a column. They are similar to FOREIGN KEY constraints in that they control the values that are put in a column. The difference is in how they determine which values are valid: FOREIGN KEY constraints obtain the list of valid values from another table, and CHECK constraints determine the valid values from a logical expression that is not based on data in another column. For example, the range of values for a salary column can be limited by creating a CHECK constraint that allows for only data that ranges from $15,000 through $100,000. This prevents salaries from being entered beyond the regular salary range.
You can create a CHECK constraint with any logical (Boolean) expression that returns TRUE or FALSE based on the logical operators. For the previous example, the logical expression is:
salary >= 15000 AND salary <= 100000
.You can apply multiple CHECK constraints to a single column. You can also apply a single CHECK constraint to multiple columns by creating it at the table level. For example, a multiple-column CHECK constraint can be used to confirm that any row with a country/region column value of USA also has a two-character value in the state column. This allows for multiple conditions to be checked in one location.
NUTSHELL
SO CHECK CONSTRAINT alone will always check for data validity but WITH CHECK will check it only against newly added constraints. However WITH NOCHECK will not validate existing data while re-enabling a constraint (but this has a potential risk as upcoming insert/update can fail).
I will suggest tp use the first statement as if there are any constraints specified on a table and when we are enabling it then its a good practice that all existing data should get validated for existing constraints.
HTH
Regards Gursethi Blog: http://ms-gursethi.blogspot.com/ ++++ Please mark "Propose As Answer" if my answer helped ++++- Proposed as answer by GURSETHI Thursday, February 10, 2011 1:59 PM
- Marked as answer by WeiLin Qiao Thursday, February 17, 2011 10:20 AM
Thursday, February 10, 2011 8:14 AM -
Here are a couple of blog posts I did on the topic a while ago:
http://sqlblog.com/blogs/tibor_karaszi/archive/2008/01/12/non-trusted-constraints.aspx
Tibor Karaszi, SQL Server MVP | web | blog- Proposed as answer by GURSETHI Thursday, February 10, 2011 1:59 PM
- Marked as answer by WeiLin Qiao Thursday, February 17, 2011 10:20 AM
Thursday, February 10, 2011 8:24 AM
All replies
-
Hi MGS,
As per Books On Line:
WITH CHECK | WITH NOCHECK
Specifies whether the data in the table is or is not validated against a newly added or re-enabled FOREIGN KEY or CHECK constraint. If not specified, WITH CHECK is assumed for new constraints, and WITH NOCHECK is assumed for re-enabled constraints.
If you do not want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. We do not recommend doing this, except in rare cases. The new constraint will be evaluated in all later data updates. Any constraint violations that are suppressed by WITH NOCHECK when the constraint is added may cause future updates to fail if they update rows with data that does not comply with the constraint.
The query optimizer does not consider constraints that are defined WITH NOCHECK. Such constraints are ignored until they are re-enabled by using ALTER TABLE table CHECK CONSTRAINT ALL.
CHECK CONSTRAINTS
CHECK constraints enforce domain integrity by limiting the values that are accepted by a column. They are similar to FOREIGN KEY constraints in that they control the values that are put in a column. The difference is in how they determine which values are valid: FOREIGN KEY constraints obtain the list of valid values from another table, and CHECK constraints determine the valid values from a logical expression that is not based on data in another column. For example, the range of values for a salary column can be limited by creating a CHECK constraint that allows for only data that ranges from $15,000 through $100,000. This prevents salaries from being entered beyond the regular salary range.
You can create a CHECK constraint with any logical (Boolean) expression that returns TRUE or FALSE based on the logical operators. For the previous example, the logical expression is:
salary >= 15000 AND salary <= 100000
.You can apply multiple CHECK constraints to a single column. You can also apply a single CHECK constraint to multiple columns by creating it at the table level. For example, a multiple-column CHECK constraint can be used to confirm that any row with a country/region column value of USA also has a two-character value in the state column. This allows for multiple conditions to be checked in one location.
NUTSHELL
SO CHECK CONSTRAINT alone will always check for data validity but WITH CHECK will check it only against newly added constraints. However WITH NOCHECK will not validate existing data while re-enabling a constraint (but this has a potential risk as upcoming insert/update can fail).
I will suggest tp use the first statement as if there are any constraints specified on a table and when we are enabling it then its a good practice that all existing data should get validated for existing constraints.
HTH
Regards Gursethi Blog: http://ms-gursethi.blogspot.com/ ++++ Please mark "Propose As Answer" if my answer helped ++++- Proposed as answer by GURSETHI Thursday, February 10, 2011 1:59 PM
- Marked as answer by WeiLin Qiao Thursday, February 17, 2011 10:20 AM
Thursday, February 10, 2011 8:14 AM -
Here are a couple of blog posts I did on the topic a while ago:
http://sqlblog.com/blogs/tibor_karaszi/archive/2008/01/12/non-trusted-constraints.aspx
Tibor Karaszi, SQL Server MVP | web | blog- Proposed as answer by GURSETHI Thursday, February 10, 2011 1:59 PM
- Marked as answer by WeiLin Qiao Thursday, February 17, 2011 10:20 AM
Thursday, February 10, 2011 8:24 AM