Adding Foreign Key Error

后端 未结 5 1484
盖世英雄少女心
盖世英雄少女心 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:02

    Enterx is right.

    For being able to detect not matching row :

    SELECT * FROM Pet p WHERE (SELECT COUNT(*) FROM Customers c WHERE c.CustomerID=p.CustomerID)=0

    Just change SELECT * by DELETE for deleting missmatching Pet entry. You can UPDATE Pet.CustomerID to NULL too. But you have to define CustomerID, from Pet table, with NULL option (and not NOT NULL)

提交回复
热议问题