<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>primary key on Tutes Club</title><link>https://tutes.in/tag/primary-key/</link><description>Recent content in primary key on Tutes Club</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Mon, 03 Jun 2019 02:59:07 +0000</lastBuildDate><atom:link href="https://tutes.in/tag/primary-key/index.xml" rel="self" type="application/rss+xml"/><item><title>How to reverse the ids in a mysql table column without breaking primary key constraint?</title><link>https://tutes.in/how-to-reverse-the-ids-in-a-mysql-table-column-without-breaking-primary-key-constraint/</link><pubDate>Sat, 01 Jun 2019 20:13:25 +0000</pubDate><guid>https://tutes.in/how-to-reverse-the-ids-in-a-mysql-table-column-without-breaking-primary-key-constraint/</guid><description>&lt;p&gt;Let me explain the problem scenario: Let us assume we have a table with 4 columns in it out of which 1 is &lt;code&gt;PRIMARY&lt;/code&gt; column and rest 3 contain some data.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h2 id="initial-data"&gt;Initial Data&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;em&gt;id&lt;/em&gt;&lt;/th&gt;
&lt;th&gt;&lt;em&gt;name&lt;/em&gt;&lt;/th&gt;
&lt;th&gt;&lt;em&gt;email&lt;/em&gt;&lt;/th&gt;
&lt;th&gt;&lt;em&gt;anything&lt;/em&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;jacob&lt;/td&gt;
&lt;td&gt;whatever&lt;/td&gt;
&lt;td&gt;how to reverse&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;nathan&lt;/td&gt;
&lt;td&gt;whosoeverisnathan&lt;/td&gt;
&lt;td&gt;ids in mysql table column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;jagmohan&lt;/td&gt;
&lt;td&gt;idontknowjagmohan&lt;/td&gt;
&lt;td&gt;without breaking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;monica&lt;/td&gt;
&lt;td&gt;iamsexy&lt;/td&gt;
&lt;td&gt;primary key constraint&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;batman&lt;/td&gt;
&lt;td&gt;idontexistinrealworld&lt;/td&gt;
&lt;td&gt;i am batman&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="data-after-update"&gt;Data after update&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;em&gt;id&lt;/em&gt;&lt;/th&gt;
&lt;th&gt;&lt;em&gt;name&lt;/em&gt;&lt;/th&gt;
&lt;th&gt;&lt;em&gt;email&lt;/em&gt;&lt;/th&gt;
&lt;th&gt;&lt;em&gt;anything&lt;/em&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;jacob&lt;/td&gt;
&lt;td&gt;whatever&lt;/td&gt;
&lt;td&gt;how to reverse&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;nathan&lt;/td&gt;
&lt;td&gt;whosoeverisnathan&lt;/td&gt;
&lt;td&gt;ids in mysql table column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;jagmohan&lt;/td&gt;
&lt;td&gt;idontknowjagmohan&lt;/td&gt;
&lt;td&gt;without breaking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;monica&lt;/td&gt;
&lt;td&gt;iamsexy&lt;/td&gt;
&lt;td&gt;primary key constraint&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;batman&lt;/td&gt;
&lt;td&gt;idontexistinrealworld&lt;/td&gt;
&lt;td&gt;i am batman&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="dude-you-are-just-reversing-the-column-what-is-so-tough-in-this"&gt;Dude, you are just reversing the column, what is so tough in this?&lt;/h2&gt;
&lt;p&gt;So, this seems simple to reverse an array if isolated by key “&lt;em&gt;id&lt;/em&gt;“. But you have to understand that this column is the primary key. So if you run a command to change id for “&lt;em&gt;jacob&lt;/em&gt;” to “&lt;em&gt;&lt;strong&gt;9&lt;/strong&gt;&lt;/em&gt;“. It will give you error: “&lt;code&gt;Duplicate entry for id 9&lt;/code&gt;“&lt;/p&gt;</description></item></channel></rss>