Doubt in SQL Table

Asked

Viewed 32 times

0

I created a table:

Tabela Curso:
Id int auto_increment
nome varchar;

Insert:
Default, Arquitetura;

Architecture has the ID 1 in the table.

I delete the course Architecture.

When I add any other course, it assumes ID = 2.

It is possible to make it occupy the ID = 1, which in theory is vacant?

  • It’s not a good idea to reuse id, especially if you have related tables.

  • The best practice is not to delete but to mark as deleted, invalid etc., recovering the sequence is bad because it would be difficult to reorder all dependent tables as stated above.

  • It makes total sense what you said. VLW

  • The question is "Is it possible to make it occupy the ID = 1, which in theory is vacant?" The answer is YES, UPDATE Course SET id='1' Where id='2' however ....

1 answer

1


Gustavo, as comments, it is not advisable to reuse code in a column with auto-increment.

However, if you come across a situation where you need to restart or set a new increment value, use the following command:

ALTER TABLE Curso AUTO_INCREMENT = 1;

In this case, the next records entered in the Course table will have the Ids incremented from 1.

You can also change the Records ID manually with an UPDATE, as commented by Leo Caracciolo.

If the table already has records, I suggest that the auto-increment value defined is the MAX+1 of the column, thus avoiding the violation of the unicity restriction in the insertion of the next records.

Browser other questions tagged

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