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

Asked

Viewed 18,077 times

5

I would like after each insert or delete from my table to use an SQL script to rearrange the Ids.

It’s kind of like this now:

Id Nome
 1 João
 3 José
 5 Ricardo

I want to leave it like this:

Id Nome
 1 João
 2 José
 3 Ricardo

How to do this the best way? (I don’t have keys connected to Id)

  • 3

    That sounds like bad practice to me. Do you have any real reason or are you just curious?

  • 1

    Be careful with that. This practice can detonate your performance database. Imagine The person of id = 3 is related in 30 more tables. By changing the id from 3 to 2, all other relationships would break with this and an update to these other tables would also be required. This type of update has no need to happen.

  • 1

    uaiHebert, as I mentioned in the question I have no key connected to Id. The table is little modified and little accessed and I know that in most cases is not a good practice. Anyway thanks for the tips.

  • 1

    I was going to ask the same thing, when I typed the title of the question I found your question. Thanks!

4 answers

19


  • Thank you! Simple and effective.

  • Just out of curiosity... what are the related tables?

  • Nice way, I didn’t know. Thank you.

  • @Nilsonuehara, this change only works if there are no related tables. If there are tables with a foreign key for this id, the bank won’t let you update.

  • Do not have headache just for perfectionism, the essential is to be functional, even with some sequence numbers missing it does not imply disorganization..

  • I used it on a whim

Show 1 more comment

2

There are 2 ways, but both are expensive to use in everyday life.

First I’ll use as an example your situation, you deleted id 4 in which case it would be something like this:

update tabela
set id = id - 1
where id > 4

Or, when it is a large amount and you don’t know exactly which ones were deleted, the "right" would be to save your data a temporary or auxiliary table. Drop your table, and create it again with the id of 0. But I don’t know if it’s viable. Or instead of dropping the entire table, simply delete the records and reset the auto-increment id like this:

ALTER TABLE 'tabela' AUTO_INCREMENT=0

0

My knowledge in SQL is not very advanced but I have learned a lot.

DECLARE @W INT = 1
DECLARE @ID INT = 1
DECLARE @Max INT = (SELECT MAX ([ID]) FROM [_Table])
WHILE @W Between 1 and @Max 
BEGIN
IF EXISTS (SELECT [ID] FROM [_Table] WHERE [ID] = @W )
BEGIN
UPDATE [_Table] SET [ID] = @ID WHERE [ID] = @W 
SET @ID +=1
SET @W +=1
END
ELSE
SET @W +=1
END

-1

Relationship with ON UPDATE CASCADE relatives keys will be changed also:

CREATE TABLE child (
    id INT, 
    parent_id INT,
    INDEX par_ind (parent_id),
    FOREIGN KEY (parent_id) 
        REFERENCES parent(id)
        ON UPDATE CASCADE
) ENGINE=INNODB;

Browser other questions tagged

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