CREATE TEMPORARY TABLE `nrds_excessive` ( SELECT `nrds`.`id` FROM `mn_NRDS` AS `nrds` LEFT JOIN `mn_Member` ON `mn_Member`.`nrds_id` = `nrds`.`id` AND `mn_Member`.`id` = `nrds`.`id_owner` WHERE `nrds`.`record_type` = 'individual' AND `mn_Member`.`id` IS NULL ); DELETE `mn_NRDS` FROM `mn_NRDS` INNER JOIN `nrds_excessive` ON `nrds_excessive`.`id` = `mn_NRDS`.`id`; DROP TABLE `nrds_excessive`; -- CREATE TEMPORARY TABLE `nrds_excessive` ( SELECT `nrds`.`id` FROM `mn_NRDS` AS `nrds` LEFT JOIN `mn_Location` ON `mn_Location`.`nrds_id` = `nrds`.`id` AND `mn_Location`.`id` = `nrds`.`id_owner` WHERE `nrds`.`record_type` = 'location' AND `mn_Location`.`id` IS NULL ); DELETE `mn_NRDS` FROM `mn_NRDS` INNER JOIN `nrds_excessive` ON `nrds_excessive`.`id` = `mn_NRDS`.`id`; DROP TABLE `nrds_excessive`; ALTER TABLE `mn_NRDS` ADD UNIQUE ( `id_owner` , `record_type` );