1
I have two tables one where the purchase order data and the second table where the items are.
Table A
id | ordem_compra | fornecedor | data
Table B
id | ordem_compra | desc | qtdade | valor | status | data
As the purchase order number is provided by the manufacturer of the item, it was all working until another industry ended up confirming us a purchase order with the same number from another factory which mixed the items when I query by the OC number.
I inserted another column in table B with the vendor code, my doubt now is as follows how to insert the vendor code of table A in table B.
I tried this way but unsuccessfully, what the right way?
UPDATE tbl_b
SET tbl_b.fornecedor =
(
SELECT tbl_a.id_fornecedor FROM tbl_a c
WHERE tbl_a.ordem_compra = c.ordem_compra
);
But is that necessary? can put the supplier in the order and the items have a key with the order table, that if it is a numerical sequence there will be no problem, hence even the table B would not need the one of the order that is already in table A, avoids this information duplicity, that model is not standardised correctly
– Ricardo Pontual
In fact, table B should have a foreign key for a unique value of table A, possibly
tabela A.id
. Keeping the purchase order value in both tables is a redundancy sign, which usually indicates that the table structure is not in the best way.– Woss
The way you did and, according to your statement, the ordem_purchase can have the same value for different suppliers, then you will also have to include supplier in table B. But since you use an artificial key (imagine id is an artificial key) then you can use such a field for the relationship but, eventually, you can generate some inconsistencies that you will have to prevent through code in your application (e.g. different id for the same pair ordem_purchase - supplier) which would not occur with the use of natural keys.
– anonimo
You are right I can put the id of table A in table B, but I didn’t understand how to do this query.
– WMomesso
I edited the question by posting the way I tried to do Insert but without success, could guide me where I missed in the query?
– WMomesso
Table B should have an id_tab_A field that would be the foreign key for the id of table A, and in this case it would not be necessary to order the field in table B. Or it should also have the vendor field, in case it uses natural keys. In both cases you need to modify the structure of your table B and reload the data.
– anonimo