Jump to content

Switching Primary Keys on an Update?


emma57573

Recommended Posts

Can you switch Primary Keys on a MYSQL PHP update? is it possible?

 

As I expected the below does not work! Reason being is that 'id' is the primary key.

 

What im trying to do here is switch the $key1 result with $key2 is there anyway to switch them at the same time without the mysql throwing an error for duplicate keys.

 

mysql_query("update thumb_images set  id=$key1 where user_id=$key2" 
);

mysql_query("update thumb_images set  id=$key2 where user_id=$key1" 
);

 

Im trying to chance the order of a thumbnail which is controled by 'id' I could add an extra field to the table and change the way thumbnails are viewed but If I can do it with a simple command to switch the "id"s it would be so much easier!

Link to comment
Share on other sites

but I dont want to change the structure of the table I just want to switch two results around for example:

 

I have a table

 

id      pid        images

 

1      10        red.gif

2      12        blue.gif

 

 

I want to do this on my PHP update

 

id      pid        images

 

1      12        blue.gif

2      10        red.gif

 

 

 

But id is the primary key in the table so if I change on an update it will coarse an error for a duplicate key. I was wondering if there was a switch comand that will switch them at the same time without returning an error

Link to comment
Share on other sites

ahhh cant do that as some of the pid's are duplicates as its a product id and the products have upto 5 images which I am trying to order.

 

My thinking now are my choices are.

 

1) Locate row 1, delete it

2) Locate row 2, update

3) re insert row 1 with the new updated id

 

Or add a new column Thumbs with a simple yes no. Meaning that yes is the primary thumbnail.

 

None of which are apealing to me really. The second would be the best option if I was starting from scratch but I hame over 7000 entries so its not that simple either....

 

Yes id is auto increase

Link to comment
Share on other sites

if you disabled auto increment your rows will require you to send an ID along with the INSERT, now, you could do without the ID row all together, however, that will lead to optimisation problems as you will have more than likely many results for 1 SELECT query, where as with an `id` you have unique rows throughout the table,,

 

If you'd like you could add me to MSN RussellonMSN[at]hotmail.com

Link to comment
Share on other sites

Thank you for your help, I'm going to return to this tomorrow after a sleep.

My script was going oh so well tonight and then this! I think yes it looks like I will have to alter the table structure slightly, I just didn't want to have to do that as its means changing other scripts about and its a right Royal pain! I dont have msn anymore but thanks for being helpful

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.