ALTER TABLE vn.invoiceOut ADD COLUMN IF NOT EXISTS customsAgentFk INT(11) DEFAULT NULL AFTER siiTrascendencyInvoiceOutFk; ALTER TABLE vn.invoiceOut ADD CONSTRAINT invoiceOut_customsAgentFk FOREIGN KEY (customsAgentFk) REFERENCES vn.customsAgent (id) ON DELETE RESTRICT ON UPDATE CASCADE; UPDATE vn.invoiceOut i JOIN vn.client c ON c.id = io.clientFk JOIN vn.address a ON a.id = c.defaultAddressFk SET i.customsAgentFk = a.customsAgentFk;