How to reverse the ids in a mysql table column without breaking primary key constraint?

Let me explain the problem scenario: Let us assume we have a table with 4 columns in it out of which 1 is PRIMARY column and rest 3 contain some data. What we want to do is to reverse the primary key IDs for that data keeping the rest of data intact. It is like shifting the first row to end and moving last row to starting. Initial Data id name email anything 1 jacob whatever how to reverse 3 nathan whosoeverisnathan ids in mysql table column 4 jagmohan idontknowjagmohan without breaking 8 monica iamsexy primary key constraint 9 batman idontexistinrealworld i am batman Data after update id name email anything 9 jacob whatever how to reverse 8 nathan whosoeverisnathan ids in mysql table column 4 jagmohan idontknowjagmohan without breaking 3 monica iamsexy primary key constraint 1 batman idontexistinrealworld i am batman Dude, you are just reversing the column, what is so tough in this? So, this seems simple to reverse an array if isolated by key “id“. But you have to understand that this column is the primary key. So if you run a command to change id for “jacob” to “9“. It will give you error: “Duplicate entry for id 9“ ...

June 1, 2019 · 2 min · Harshvardhan Malpani