You say that you cannot change the structure of the table. If you could, I would recommend against the denormalization (duplication of rows) of these tables depending on what you are doing.
I'm also a little confused because you are saying that you need to duplicate strings and relationships from id_category = 1 to id_category = 3 , but then your query samples have id_category = 2 .
However, this answer should be applied independently. I would use PHP and PDO.
$pdo = new PDO('mysql:host=?', $user, $passwd); $stmtCustomers = $pdo->prepare("INSERT INTO customer (t_name, id_category) VALUES (t_name, ?) WHERE id_category = ?"); $stmtContracts = $pdo->prepare("INSERT INTO contract (t_name, id_category) VALUES (t_name, ?) WHERE id_category = ?"); $stmtRelation = $pdo->prepare("INSERT INTO customer_has_contract VALUES (?, ?)"); //Perform in a loop if needed $pdo->beginTransaction(); $stmtCustomers->execute($target_cat_id, $origin_cat_id); $cus_id = $pdo->lastInsertId(); $stmtContracts->execute($target_cat_id, $origin_cat_id); $con_id = $pdo->lastInsertId(); $stmtRelation->execute($con_id, $cus_id); $pdo->commit();
source share