ALTER TABLE vn.supplier
	CHANGE companySize companySize enum('small','medium','big') CHARACTER SET utf8mb3
		COLLATE utf8mb3_general_ci DEFAULT NULL NULL AFTER stamp,
	ADD geoFk int(11) DEFAULT NULL NULL AFTER companySize,
	ADD CONSTRAINT supplier_zoneGeo_FK FOREIGN KEY (geoFk)
			REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE;

CREATE OR REPLACE TEMPORARY TABLE tSupplierGeo
	(PRIMARY KEY (id))
	ENGINE = MEMORY
	SELECT sa.supplierFk id, p.geoFk
		FROM supplierAddress sa
			JOIN town t ON t.provinceFk = sa.provinceFk
			JOIN postCode p ON p.townFk = t.id
				AND p.`code` = sa.postalCode
		GROUP BY sa.supplierFk
		ORDER BY (sa.city SOUNDS LIKE t.`name`) DESC;

UPDATE supplier s
		JOIN tSupplierGeo tsg ON tsg.id = s.id
	SET s.geoFk = tsg.geoFk;

DROP TEMPORARY TABLE tSupplierGeo;
