ALTER TABLE vn.invoiceOut ADD COLUMN IF NOT EXISTS customsAgentFk INT(11) DEFAULT NULL AFTER siiTrascendencyInvoiceOutFk; ALTER TABLE vn.invoiceOut ADD COLUMN IF NOT EXISTS incotermsFk varchar(3) DEFAULT NULL AFTER customsAgentFk; ALTER TABLE vn.invoiceOut ADD CONSTRAINT invoiceOut_customsAgentFk FOREIGN KEY (customsAgentFk) REFERENCES vn.customsAgent (id) ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE vn.invoiceOut ADD CONSTRAINT invoiceOut_incotermsFk FOREIGN KEY (incotermsFk) REFERENCES vn.incoterms (`code`) ON DELETE RESTRICT ON UPDATE CASCADE; UPDATE vn.invoiceOut io JOIN vn.client c ON c.id = io.clientFk JOIN vn.ticket t ON t.clientFk = c.id JOIN vn.address a ON a.id = t.addressFk SET io.customsAgentFk = a.customsAgentFk, io.incotermsFk = a.incotermsFk;