-1

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?

Arulkumar
  • 12,966
  • 14
  • 47
  • 68

1 Answers1

1

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.

Ionic
  • 3,884
  • 1
  • 12
  • 33
  • 1. No they are of the same type , but the second question is yes , when i execute the query i got some result , what should i do? – Besh Mohammed Jun 18 '15 at 08:10
  • You have just two options. Remove the rows from the product table as they have no mapped customer. Or create a customer and map those rows to the newly created customer. Afterwards create your foreign key. – Ionic Jun 18 '15 at 08:13
  • No problem. Did this work for you? If so, you can mark the answer as your solution. (I see that your a new user :)) – Ionic Jun 18 '15 at 08:30