Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何消除向包含数据的表中添加外键的错误_Sql Server_Foreign Keys_Constraints_Alter Table - Fatal编程技术网

Sql server 如何消除向包含数据的表中添加外键的错误

Sql server 如何消除向包含数据的表中添加外键的错误,sql-server,foreign-keys,constraints,alter-table,Sql Server,Foreign Keys,Constraints,Alter Table,我正在使用以下书面查询将外键添加到包含数据的现有字段中: ALTER TABLE ServiceDetails ADD CONSTRAINT fk_ServiceDetails FOREIGN KEY (OCF_ID) REFERENCES OCF_Commerce(OCF_Internal_ID); 但是在执行查询之后,我得到了一个错误。我错过了什么 错误消息是: 味精547,第16级,状态0,第1行 ALTER TABLE语句与外键约束“fk_ServiceDetails”冲突。冲

我正在使用以下书面查询将外键添加到包含数据的现有字段中:

 ALTER TABLE ServiceDetails
 ADD CONSTRAINT fk_ServiceDetails
 FOREIGN KEY (OCF_ID)
 REFERENCES OCF_Commerce(OCF_Internal_ID);
但是在执行查询之后,我得到了一个错误。我错过了什么

错误消息是:

味精547,第16级,状态0,第1行
ALTER TABLE语句与外键约束“fk_ServiceDetails”冲突。冲突发生在数据库“TrulyDB”、表“dbo.OCF_Commerce”、列“OCF_Internal_ID”中


您的表中可能有不一致的数据 试着跑步

select *
from ServiceDetails 
where OCF_ID not in (select OCF_Internal_ID from OCF_Commerce)

这将输出主表中缺少ID的所有行

检查您的ServiceDetails表是否在OCF_Commerce Tbaledata中包含FK和PK的所有OCF_ID和OCF_Internal_ID不匹配,例如FK为194,但PK为194。