Adding Foreign Key Error

后端 未结 5 1472
盖世英雄少女心
盖世英雄少女心 2021-01-26 07:37

I want to add a foreign key from Table Customers, row= \"Customer ID\" to Table Pet, row= \"Customer ID\".

-- Table struct         


        
5条回答
  •  旧巷少年郎
    2021-01-26 08:11

    It looks like to me that you inserted values in table Pet's column ID when they should have been inserted in CustomerID and vice-versa.

    By the way, it's not really good to have IDs as VARCHARs, specially when they are foreign keys. This makes queries processing slower, although your tables don't look like they'll have a huge number of rows for this to make a difference. Anyway, it's just an observation. I would consider have artificial int primary keys in my tables.

    EDIT

    I had misread table Pet's values. The other answers here are right. You need to update those 0 values in CustomerID column to match existing CustomerIDs in Customer table or delete them, otherwise you'll get an error when trying to create the FK.

提交回复
热议问题