WITH CHECK关键字有什么用?

时间:2014-05-30 04:11:53

标签: sql .net sql-server-2008

ALTER TABLE Employee
CHECK CONSTRAINT CK_Employee_BirthDate

当我运行上面的代码行时,我没有收到错误。

但是,当我在代码行下面运行时,我收到错误" ALTER TABLE语句与FOREIGN KEY约束冲突"

ALTER TABLE Employee
WITH CHECK CHECK CONSTRAINT CK_Employee_BirthDate

任何人都可以帮助我理解使用WITH CHECK和不使用它之间有什么区别吗?

1 个答案:

答案 0 :(得分:0)

关注波格丹的评论,http://msdn.microsoft.com/en-us/library/ms190273.aspx解释了" WITH CHECK"语法确保表中的数据针对新的CHECK约束进行验证。从该链接开始,假定WITH CHECK用于新约束,但在重新启用现有约束时假定为WITH NOCHECK:

 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 WITH CHECK CHECK CONSTRAINT ALL.