简体   繁体   English

SQL Server 检查约束可为空

[英]SQL Server Check Constraint Nullable

For any check constraint if the BOOLEAN_EXPRESSION returns true, then the CHECK constraint allows the value, otherwise it doesn't.对于任何检查约束,如果 BOOLEAN_EXPRESSION 返回 true,则 CHECK 约束允许该值,否则不允许。 For a null-able column, it's possible to pass null for this column, when inserting a row.对于可为空的列,在插入行时,可以为此列传递空值。 When you pass NULL for the AGE column, the Boolean expression evaluates to UNKNOWN, and allows the value.But what will happen in case of a Non null-able column if I pass null ?当您为 AGE 列传递 NULL 时,布尔表达式的计算结果为 UNKNOWN,并允许该值。但是,如果我传递 null ,在不可为空的列的情况下会发生什么?

For any check constraint if the BOOLEAN_EXPRESSION returns true, then the CHECK constraint allows the value, otherwise it doesn't.对于任何检查约束,如果 BOOLEAN_EXPRESSION 返回 true,则 CHECK 约束允许该值,否则不允许。

This is incorrect.这是不正确的。 A check constraint returns a check validation error only when the boolean expression evaluates to FALSE.仅当布尔表达式的计算结果为 FALSE 时,检查约束才会返回检查验证错误。 NULL does not generate an error. NULL不会产生错误。

This is different behavior from WHERE and CASE WHEN clauses.这与WHERECASE WHEN子句的行为不同。

If the column is non-NULLable, then you will have a violation of the not- NULL constraint.如果该列不可为 NULL,那么您将违反非NULL约束。 I think that is checked before the CHECK constraint, but the ordering doesn't matter.我认为这是在CHECK约束之前CHECK ,但顺序无关紧要。 You will get a nullability error.您将收到可空性错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM