0

How can I re order id number in MySQL database if I delete a record?

Example

Id 1 - data information for row

Id 5 - more data information

Id 8 - and more

See how I deleted 2,3,4,6,7 id number rows. Is there a way to automatically Re order rows automatically if I delete a row. Like change the above to 1,2,3 id number but keep other data there

Thanks in advance for your assistance

Dharman
  • 30,962
  • 25
  • 85
  • 135
J D
  • 51
  • 7
  • 1
    if it is a primary key then dont change it. use serialize number if you want just to display. – Devsi Odedra Aug 06 '19 at 04:24
  • 1
    Any specific reason you want to do this? – Moses Schwartz Aug 06 '19 at 04:26
  • 'Is there a way to automatically Re order' - no there isn't. And it can be a bit tricky if id is used as a foreign key anywhere. This question does come up frequently try searching for mysql reset auto_increment. – P.Salmon Aug 06 '19 at 06:21

1 Answers1

0

You should not update the primary key value in any table. If you are deleting rows at the end and want the counter to start at the max number in the table plus one, then you can reset that if you want to. Found an article that already gives the syntax:

How to reset AUTO_INCREMENT in MySQL?.

Polski
  • 13
  • 5