Search code examples
mysqlphpmyadminforeign-key-relationship

PhpMyAdmin: How to find the foreign key that is missing in the parent table?


In a mySQL database I have two populated tables. Their structures are as in the image. When trying to relate them on idArticol (in PhpMyAdmin), an error appears: "Relation could not be added". I have more than 6000 records in each table. Apparently, the error comes from a missing foreign key from the parent table. Is there an SQL command that can help me find this missing key?

enter image description here


Solution

  • You can find the missing key by using this command

    SELECT * FROM userart WHERE idArticol NOT IN (SELECT idArticol FROM articol)

    This will give you all entrys in userart having an idArticol that is not in articol.idArticol