title: mysql update throwing Error Code: 1062. Duplicate entry
body: <p>I´m getting an error on the table 'telefono' :  <code>Error Code: 1062. Duplicate entry '84-1' for key 'PRIMARY'</code> when the stored procedure is called and they are in the opposite order in the table.</p>
  
  <p>For example if i do <code>call updateProvider("1", "Terra1", "Ana", "Mora", "Escobar", "31312323", "11111111");</code> and the numbers 31312323 and 11111111 already have an entry in the table <code>telefono_proveedor</code> in the opposite order it throws the error.</p>
  
  <p>The error corresponds to this update</p>
  
  <pre><code>UPDATE telefono_proveedor SET id_telefono = var_id_phone1
          WHERE id_telefono = old_id_phone1 AND id_proveedor = var_id_prov;
  UPDATE telefono_proveedor SET id_telefono = var_id_phone2
          WHERE id_telefono = old_id_phone2 AND id_proveedor = var_id_prov;
  </code></pre>
  
  <p>SQL of the table :</p>
  
  <pre><code>CREATE TABLE `telefono` (
    `id_telefono` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `tipo` enum('casa','movil','oficina') NOT NULL,
    `numero` varchar(20) NOT NULL,
    PRIMARY KEY (`id_telefono`),
    UNIQUE KEY `id_telefono_UNIQUE` (`id_telefono`),
    UNIQUE KEY `numero_UNIQUE` (`numero`),
    UNIQUE KEY `numero` (`numero`)
  ) ENGINE=InnoDB AUTO_INCREMENT=119 DEFAULT CHARSET=utf8;
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/PShaI.jpg" rel="nofollow">This is the diagram of the table</a></p>
  
  <p>Stored Procedure:</p>
  
  <pre><code>CREATE DEFINER=`root`@`localhost` PROCEDURE `updateProvider`(
  IN var_id_prov INT(10), IN var_provider VARCHAR(20),
  IN var_rep_name VARCHAR(20), IN var_rep_last_name VARCHAR(20), 
  IN var_rep_last_name2 VARCHAR(20), IN var_phone1 VARCHAR(20), 
  IN var_phone2 VARCHAR(20)
  )
  BEGIN
  
  DECLARE old_id_phone1 INT(10);
  DECLARE old_id_phone2 INT(10);
  DECLARE var_id_phone1 INT(10);
  DECLARE var_id_phone2 INT(10);
  
  SELECT id_telefono INTO old_id_phone1 FROM telefono_proveedor
      WHERE id_proveedor = var_id_prov ORDER BY id_telefono ASC LIMIT 1;
  SELECT id_telefono INTO old_id_phone2 FROM telefono_proveedor
      WHERE id_proveedor = var_id_prov ORDER BY id_telefono ASC LIMIT 1,1;
  
  INSERT IGNORE INTO telefono (tipo, numero) VALUES ('oficina', var_phone1);
  INSERT IGNORE INTO telefono (tipo, numero) VALUES ('oficina', var_phone2);
  
  SELECT id_telefono INTO var_id_phone1 FROM telefono WHERE numero = var_phone1;
  SELECT id_telefono INTO var_id_phone2 FROM telefono WHERE numero = var_phone2;
  
  UPDATE telefono_proveedor SET id_telefono = var_id_phone1
      WHERE id_telefono = old_id_phone1 AND id_proveedor = var_id_prov;
  UPDATE telefono_proveedor SET id_telefono = var_id_phone2
      WHERE id_telefono = old_id_phone2 AND id_proveedor = var_id_prov;
  
  UPDATE proveedor SET nombre_proveedor = var_provider, nombre_representante = var_rep_name, 
      apellido1_representante = var_rep_last_name, apellido2_representante = var_rep_last_name2
      WHERE id_proveedor = var_id_prov;
  
  END
  </code></pre>