批量导入后检查约束

Checking constraints after bulk import

我的问题与 this one 非常相似,但适用于自适应服务器企业。我使用 BCP 将数据加载到数据库中。在批量导入期间,不执行外键约束检查。

导入数据后检查外键约束的最佳方法是什么?

我不知道 Sybase ASE 中的任何命令类似于 MSSQL 的 alter table/check check constraint

关于如何强制检查 FK 约束的一些想法:

  • bcp 到暂存区 table,然后通过从暂存区 table 选择插入到目标 table;这会引发错误,但不一定会告诉您哪些行未通过 FK 检查(除非您选择一次 insert/select 1 行)

  • 用当前值更新 FK 列; ASE 不够聪明,无法知道 before/after 值相同,所以它会 运行 FK 检查;同样,这会产生一个错误,但不会告诉您哪些行未通过 FK 检查;如果它确实遍历并更新一堆行(和相关索引),它也不是很有效;类似于:

update child_table set fk_col = fk_col where ... for-the-child-rows-just-inserted ...

  • 考虑 运行宁 not exists 查询以查找未通过 FK 检查的行,例如:

select * from child c where not exists(select 1 from parent p where p.pk = c.fk) and ... for-the-child-rows-just-inserted ...