外键列允许 SQL 服务器中的 0 来自批量插入

Foreign key column allows 0 in SQL Server from bulk insert

我有一列检查列上的 FK 约束并允许 NULL。但是,它允许 0 作为值,即使在主 table 中没有 ID 为 0 的行。

此插入发生在使用 .NET 的批量插入上。但是,如果我将值更改为有效数字并直接在 SQL 服务器中返回 0,那么它将不允许它。

所以批量插入以某种方式允许 0 被允许,有什么原因吗?

默认情况下,BULK INSERT 忽略检查和外键约束。您可以使用 CHECK_CONSTRAINTS.

打开它们

这在文档中有解释:

CHECK_CONSTRAINTS

Specifies that all constraints on the target table or view must be checked during the bulk-import operation. Without the CHECK_CONSTRAINTS option, any CHECK and FOREIGN KEY constraints are ignored, and after the operation, the constraint on the table is marked as not-trusted.