【问题标题】:What is the use of WITH CHECK keywordWITH CHECK 关键字有什么用
【发布时间】:2014-07-19 18:13:03
【问题描述】:
ALTER TABLE Employee
CHECK CONSTRAINT CK_Employee_BirthDate

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

但是当我在下面的代码行运行时出现错误“ALTER TABLE 语句与 FOREIGN KEY 约束冲突

ALTER TABLE Employee
WITH CHECK CHECK CONSTRAINT CK_Employee_BirthDate

谁能帮我理解使用 WITH CHECK 和不使用它有什么区别?

【问题讨论】:

标签: sql .net sql-server-2008


【解决方案1】:

跟进 Bogdan 的评论,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.

【讨论】:

    猜你喜欢
    • 2017-11-21
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    相关资源
    最近更新 更多