Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Valores de chave primária de troca do MySQL


Não use tabelas temporárias para isso.

Do manual :
mysql> SELECT * FROM temp_table, temp_table AS t2;
ERROR 1137: Can't reopen table: 'temp_table'

ATUALIZAÇÃO:

Desculpe se não entendi direito, mas por que uma simples troca de três vias não funciona?

Assim:
create table yourTable(id int auto_increment, b int, primary key(id));

insert into yourTable(b) values(1), (2);
select * from yourTable;

DELIMITER $$
create procedure pkswap(IN a int, IN b int)
BEGIN
select @max_id:=max(id) + 1 from yourTable;
update yourTableset [email protected]_id where id = a;
update yourTableset id=a where id = b;
update yourTableset id=b where id = @max_id;
END $$
DELIMITER ;

call pkswap(1, 2);

select * from yourTable;