在 sql 服务器中 link 2 table 时出错

Error when link 2 table in sql server

我正在尝试在 sql 服务器中 link 2 table 让我们假设其中一个名为 customer 而另一个是 product.

customercustID 作为主键,我想 link 它与 product table 中的 custID 作为外键,它给了我这个错误:

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_product_customer". The conflict occurred in database "xyz", table "dbo.customer", column 'custID #'.

有没有人知道,我该如何解决这个问题?

这可能有很多原因。

  1. 这些列之间是否存在类型不匹配?
  2. 产品中的每个客户是否也在您的客户中table?

您可以使用此检查第二部分:

SELECT DISTINCT p.customer_id
FROM products as p
LEFT JOIN customers as c ON p.customer_id = c.customer_id
WHERE c.customer_id is null

希望这能给你一个提示。