How to renumber table Ids in Mysql?

Asked

Viewed 1,386 times

0

I deleted from my sales chart the test sales records made by me, and the sales canceled. Then the sales numbering became disorderly, like: it starts with 9, 10, 11, 14.... How do I order sales from #1 and so on?

  • 1

    Give more details of how you are and where you want to get to. In the current form the question cannot be answered.

  • I think what @bigown is wondering is, what does this "numbering" column look like? Is it the primary key? Is it autoincremented? That said, in general the table ID ends up having these holes even, I would leave as it is.

  • começa com 9, 10, 11, 14.... I answered, but 11 jumped to 14?

  • @Edi.Gomes from what I understood these deleted id’s were some test sales he himself made.

  • @Rodrigo Rigotti That’s what I understood, he wants to start from scratch in production, must have been confused in the example.

  • I still think this question has to be rephrased, the whole issue has become the biggest mess.

Show 1 more comment

2 answers

2

Another solution to the scenario presented, ONLY if there are no relationships in this table:

SET @contador = 0;
UPDATE `tabela_vendas` SET `tabela_vendas`.`id` = @contador := @contador + 1;

Source: How to organize auto-increment numbering of a column Id of a table in Mysql?

After this, run the command below, setando again the initial number of auto_imcrement to 0:

ALTER TABLE tabela_vendas AUTO_INCREMENT = 0
  • Remembering that then should be used the ALTER TABLE mentioned by Edi, using the id major + 1 as next AUTO_INCREMENT, otherwise there will be a "hole" when entering the next record.

  • @Bacco, well remembered. I will add in my reply.

1

I think you want to reformulate the AUTO_INCREMENT. Try:

ALTER TABLE sua_tabela AUTO_INCREMENT = 0
  • 1

    I believe he also wants to renumber the existing records. However, it is worth discussing the validity of doing this...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.