CREATE TABLE IF NOT EXISTS `mm_Company2County` ( `companyId` int(10) unsigned NOT NULL, `countyId` int(10) unsigned NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; INSERT INTO `mm_Company2County` SELECT id, countyId FROM `mm_Company` WHERE countyId IS NOT NULL; ALTER TABLE `mm_Company2County` ADD PRIMARY KEY (`companyId`,`countyId`), ADD KEY `countyId` (`countyId`); ALTER TABLE `mm_Company2County` ADD CONSTRAINT `mm_Company2County_ibfk_2` FOREIGN KEY (`countyId`) REFERENCES `mm_County` (`id`) ON UPDATE CASCADE, ADD CONSTRAINT `mm_Company2County_ibfk_1` FOREIGN KEY (`companyId`) REFERENCES `mm_Company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE `mm_Company` DROP FOREIGN KEY `mm_Company_ibfk_2`; ALTER TABLE `mm_Company` DROP `countyId`;