The primary key in the table is an integer with the auto_increment option. I deleted an entry in the table with id (primary key) 4. Id of the next entry remained 5, next 6, etc. How to overwrite the id of each entry?

  • 3
    what is the point you see in this? besides wasting resources ... - splash58

1 answer 1

This can not be done.

The purpose of the primary key is to uniquely identify a table entry. And throughout the life of the table. It is the tables, not the records - that is, even if the record is removed from it, the PC value of this record should never be used again.

Want continuous numbering? set up a separate field for this and mock him as you please, for example:

-- создание поля ALTER TABLE `table` ADD COLUMN numbering BIGINT NOT NULL; -- инициализация начального значения (на 1 меньше) SET @num := 0; -- нумерация поля с требуемым порядком сортировки UPDATE `table` SET numbering = (@num := @num + 1) ORDER BY id ASC; 

Clearly, the second and third operations can be performed at any time when renumbering is required, incl. changing the sorting required (direction and / or field).

Do not touch the primary key.

  • Then there is how to implement it? I have an ArrayList in which there must be the same records as in the table (but only one field). But my id in ArrayList does not match the id in the table. This interferes with the fact that when I want to delete something, I get the id from the ArrayList and pass it to the table. - KotFind
  • когда я хочу что-то удалить, я получаю id из ArrayList и передаю его в таблицу. So pass not id, but value ... - Akina