Update field of a table with the value that same field has in another table

2

I have a table of items to sell ( ARTICULOS_TBL ) that I sort in a field of that table by family (field CODFAM ). That is to say, it is the case that there are a large number of articles that belong to the same family. In total there will be around 18,000 items. However, it has been decided to change the classification criteria of families, since it has been found to be impractical. For this I have created a new table ( CONVERSION_TBL ) in which I do the "conversion": to each family code ( CODFAM ) I associate a new value in that table ( CODFAM_NEW ). For this I have created a query like the following, which does not like:

UPDATE articulos_tbl  A
SET A.codfam=
       (SELECT CONVERSION_TBL.codfam_new
        FROM CONVERSION_TBL
        WHERE CONVERSION_TBL.codfam=A.codfam)
WHERE EXISTS (SELECT CONVERSION_TBL.codfam
              FROM CONVERSION_TBL
              WHERE CONVERSION_TBL.codfam=at_articulos_tbl.codfam);

The error he gives me is the following, but I do not understand it very well (I do not have that much level):

"Integrity cosntrains violated - parent key not found.

Cause: A foreign key value has no matching primary key value
Action: Delete the foreign key or add a matching priumary key."

Help ... !! and thank you very much.

    
asked by Kike Astudillo 02.10.2018 в 13:21
source

1 answer

0

You are updating data in the articles_tbl table that refers to another table which does not include this data as a primary key.

It is not very easy to say which field can be without seeing the structure of articulos_tbl .

As suggested by the database engine (it seems to be PostgreSQL) you can delete the foreign key, do the update and then re-create the foreign key, but it will give you the error again because the value is not included referenced in the other table, but maybe so, it will be easier for you to find the missing value.

Greetings and I hope it will be helpful.

    
answered by 02.10.2018 в 13:34