ALTER TABLE vn.agencyTerm ADD supplierFk INT NULL;
ALTER TABLE vn.agencyTerm CHANGE supplierFk supplierFk INT NULL AFTER agencyFk;

UPDATE vn.agencyTerm `at`
	JOIN vn.agency a ON a.id = `at`.agencyFk
SET `at`.supplierFk = a.supplierFk;

ALTER TABLE vn.agencyTerm ADD CONSTRAINT agencyTerm_FK FOREIGN KEY (agencyFk) REFERENCES vn.agency(id) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE vn.agencyTerm ADD CONSTRAINT agencyTerm_FK_1 FOREIGN KEY (supplierFk) REFERENCES vn.supplier(id) ON DELETE RESTRICT ON UPDATE CASCADE;

RENAME TABLE vn.agencyTerm TO vn.supplierAgencyTerm;

CREATE OR REPLACE
ALGORITHM = UNDEFINED 
DEFINER=`root`@`localhost`  
VIEW `vn`.`agencyTerm` AS
SELECT
    `sat`.`agencyFk` AS `agencyFk`,
    `sat`.`minimumPackages` AS `minimumPackages`,
    `sat`.`kmPrice` AS `kmPrice`,
    `sat`.`packagePrice` AS `packagePrice`,
    `sat`.`routePrice` AS `routePrice`,
    `sat`.`minimumKm` AS `minimumKm`,
    `sat`.`minimumM3` AS `minimumM3`,
    `sat`.`m3Price` AS `m3Price`
FROM
    `vn`.`supplierAgencyTerm` `sat`
