ALTER TABLE `mn_NRDS` ADD `id_company` INT NOT NULL AFTER `record_type` ; ##setting company id for nrds dictinary # locations UPDATE mn_NRDS `nrds` SET id_company = (SELECT `id_company` FROM mn_Location `l` WHERE `l`.`id` = `nrds`.`id_owner`) WHERE `nrds`.`record_type` = 'location'; # members UPDATE mn_NRDS `nrds` SET id_company = (SELECT `id_company` FROM mn_Location `l` WHERE `l`.`id` = (SELECT `id_location` FROM mn_Member AS `m` WHERE `m`.`id` = `nrds`.`id_owner`) ) WHERE `nrds`.`record_type` = 'individual'; ALTER TABLE `mn_NRDS` DROP PRIMARY KEY , ADD PRIMARY KEY ( `id` , `id_owner` , `record_type` ); ALTER TABLE `mn_NRDS` CHANGE `id_company` `id_company` INT( 11 ) NULL DEFAULT NULL; UPDATE `mn_NRDS` SET `id_company` = NULL WHERE `id_company` = 0; ALTER TABLE `mn_NRDS` ADD INDEX ( `id` , `id_company` );