Can i adjust the value of an auto-incremented field in the database automatically?

走远了吗. 提交于 2021-02-19 06:31:33

问题


Can i adjust the value of an auto-incremented field in the database automatically?

I have a table called "post" which has a field called "pid" which is set to auto-increment. Posts from this table may be deleted by the user at a later time, but the auto- incremented value will not be adjusted. Is there a way to adjust the pid field everytime posts are deleted?

for eg:If i have 4 entries: pid=1,2,3,4(pid-auto-increment) Now if i delete 2, is there a way to update 3 to 2 and 4 to 3 and so on ?


回答1:


Why would you need to adjust the auto-increment? Each post is uniquely identified using the pid and if that is to change, then the whole DB structure will fail. The idea of the auto-increment is based on this principle and that you don't have to worry about assigning numbers yourself.

If deleting a record is a problem, then you might want to keep it in the database and flag it as deleted. They you can use this flag to show / hide from the users.




回答2:


Deletion from end

You can manually set AUTO_INCREMENT of a table to a specified value via

ALTER TABLE tbl AUTO_INCREMENT = val;

See Using AUTO_INCREMENT in MySQL manual.

This solves deletion from end – before adding new rows, set AUTO_INCREMENT to 0 and it will be automatically set to current maximum plus one. Newly inserted rows will occupy the same IDs as the deleted ones.

Deletion from anywhere – renumbering

It is possible to manually specify value of the field having AUTO_INCREMENT. AUTO_INCREMENT is ignored them. If you specify a value already used, unique constraint will abort the query. If you specify a value that is bigger than the current maximum, AUTO_INCREMENT automatically set to this one plus one.

If you do not want to manually renumber the records, write a script for that, nor mess with stored procedures, you can use user-defined variables:

SET @id = 0;
UPDATE tbl SET id = @id := @id + 1 ORDER BY id;
SET @alt = CONCAT('ALTER TABLE tbl AUTO_INCREMENT = ', @id + 1);
PREPARE aifix FROM @alt;
EXECUTE aifix;
DEALLOCATE PREPARE aifix;

Example use

  • http://www.paulwhippconsulting.com.au/webdevelopment/31-renumbering-an-qorderingq-field-in-mysql
  • http://www.it-iss.com/mysql/mysql-renumber-field-values/

For more info see my answer to a related question.

Warning – this may be harmful!

Usually there is no need to renumber the records. Actually it may be harmful as you may have dangling references to the old record somewhere (possibly outside the DB) and they now become valid again, which could cause confusion. This is why AUTO_INCREMENT attribute of the table is not decremented after a row is deleted.

You should just delete the unwanted records and stop worrying about the holes. They are just in the numbering of the records, purely logical, physically they don’t need to exist in the storage. No space wasted in the long time perspective. For some time the storage really has holes. You can let the DB engine get rid of them by OPTIMIZE TABLE tbl or ALTER TABLE tbl ORDER BY column.



来源:https://stackoverflow.com/questions/21646630/can-i-adjust-the-value-of-an-auto-incremented-field-in-the-database-automaticall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!