MySQL Forums
Forum List  »  PHP

Need to swap primary keys - does mysql have a way?
Posted by: matt snider
Date: March 31, 2006 06:21AM

Hey all, I couldn't find this in the cookbook.

I need to swap the primary keys of two elements. My technique is to pick an absurdly large primary key value and change elem1 to that, then change elem2 pk to the pk for elem1, then change elem1 pk to the pk of elem2.

$pk = 10;
$pk2 = 5;
$tpk = 99999999 - $pk;

$mysql->update( $table, "pk = $tpk ", "pk = ".$pk);
$mysql->update( $table, "pk = ".$pk, "pk = ".$idpk2);
$mysql->update( $table, "pk = $pk2", "pk = ".$tpk);

However, this causes the autoincrement feature to start incrementing in the absurdly high numbers, which defeats the purpose of choosing such a number.

a) is there a function that swaps the pk's of two elements in mysql?

or b) is there a way to reset the autoincrement feature back to the low numbers after the swap?

or c) is there a way to randomly get a pk from the database, that isn't already in use?

Options: ReplyQuote


Subject
Written By
Posted
Need to swap primary keys - does mysql have a way?
March 31, 2006 06:21AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.