Error when link 2 table in sql server

70 views Asked by At

I am trying to link 2 table in sql server let's assume one of them called customer and the other is product.

customer has custID as primary key and i want to link it with custID in product table as foreign key, and its give me this error:

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

Is there anyone know, how can I fix the issue?

1

There are 1 answers

3
Ionic On BEST ANSWER

This could have many reasons.

  1. Do you have a type missmatch between those columns?
  2. Is every customer in products also in your customer table?

You can check the second part by using this:

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

Hope this give you a hint.