简体   繁体   English

高级使用自动增量?

[英]Advanced use of auto-increment?

I'll try and make this question as brief as possible. 我将尽力使这个问题尽可能简短。 I was wondering if there is a way to do the following via PHP & MySQL coding: 我想知道是否有一种方法可以通过PHP和MySQL进行编码:

  1. Delete an auto-incremented id entry (say entry 12 of 20), and at the same time lower the rest of the entries by 1. (Example: Delete entry 12 == Entry 13 becomes 12, 14 becomes 13, etc. etc.) 删除一个自动递增的id条目(例如20中的条目12),同时将其余条目降低1。(例如:删除条目12 ==条目13变为12,14变为13,依此类推。 )

  2. Manually (ie via form) insert an entry with an ID so that the rest are pushed up if specified by the user (and defaults to normal behaviour if nothing is specified. (Example: User specifies that he wants his entry to be ID 14, but there is already an ID 14 in the database. What should happen would be that the user input pushes the already existing entry up to 15, & the rest of the existing entries follow a similar fashion. If the user does not specify an ID, default to normal behaviour.) 手动(即通过表单)插入一个具有ID的条目,以便在用户指定时将其向上推(如果未指定任何内容,则默认为正常行为。(示例:用户指定他希望其条目为ID 14,但数据库中已经有一个ID 14,应该发生的情况是,用户输入将已经存在的条目推到15,而其余的现有条目也采用类似的方式。如果用户未指定ID,默认为正常行为。)

I'm still learning MySQL & PHP and the code I need is for an image gallery system that I've been building for a while now. 我仍在学习MySQL和PHP,我需要的代码是一个我已经建立一段时间的图库系统。

Is it at all possible? 有可能吗?

That is really not an efficient way to use auto-increment columns (databases are not designed to do that). 这实际上不是使用自动增量列的有效方法(数据库并非旨在这样做)。 You'd wind up doing effectively a table scan any time you inserted or removed an item. 每当您插入或移除项目时,您都可以有效地进行表格扫描。

If you want to have an ordering that you can insert at will, it'd be better to maintain a separate mapping (eg have a separate entry that stores a list of IDs that you can manipulate at will). 如果您希望可以随意插入一个命令,则最好维护一个单独的映射(例如,有一个单独的条目存储可以随意操作的ID列表)。

For task 1, you would need to manually set the AUTO_INCREMENT value for that table after modifying all of the IDs so there's no gap when the next insert happens. 对于任务1,您需要在修改所有ID之后手动为该表设置AUTO_INCREMENT值,以便在下一次插入时没有间隙。 A far better way is to not use an auto-increment ID at all - have an ID column, and use MAX(id)+1 to find the required ID at the time of record insertion. 更好的方法是根本不使用自动递增的ID-拥有一个ID列,并在插入记录时使用MAX(id)+1查找所需的ID。

here's my psuedocode: 这是我的伪代码:

for a deletion: 删除:

$n = 12;
DELETE from items where id='$n';
UPDATE items SET id=id-1 WHERE id > '$n'

for a specified insertion: 对于指定的插入:

$n = 15;
(if there is a record with id = 15)
   UPDATE items SET id=id+1 WHERE id >= '$n'
INSERT into items (id,..) values ('$n',...)

for an unspecified insertion 用于未指定的插入

$n = SELECT MAX(id)+1 FROM items
INSERT into items (id,..) values ('$n',...)

For #1 try this SQL commands: 对于#1,请尝试以下SQL命令:

DELETE FROM table WHERE id=12;
UPDATE table set id=id-1 WHERE id > 12;
ALTER TABLE table AUTO_INCREMENT=AUTO_INCREMENT-1;

But it is a bad idea to use ID field like this. 但是使用这样的ID字段是一个坏主意。 You shouldn't modify it's value after row was inserted into table. 在将行插入表后,您不应修改其值。

You can use this for your 1) question. 您可以将其用于您的1)问题。 Run the whole block together. 一起运行整个块。 @id should be set to the row you want to delete. @id应该设置为要删除的行。

SET @id = 12;
DELETE FROM tbl WHERE id = @id;
UPDATE  tbl set id = id-1 WHERE id > @id;
ALTER TABLE tbl AUTO_INCREMENT = 0;

MySQL disclaimer: You cannot reset the counter to a value less than or equal to any that have already been used. MySQL免责声明:您不能将计数器重置为小于或等于已使用的任何值。 For MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column, the value is reset to the current maximum plus one. 对于MyISAM,如果该值小于或等于AUTO_INCREMENT列中当前的最大值,则该值将重置为当前的最大值加1。 For InnoDB, you can use ALTER TABLE ... AUTO_INCREMENT = value as of MySQL 5.0.3, but if the value is less than the current maximum value in the column, no error occurs and the current sequence value is not changed. 对于InnoDB,您可以使用ALTER TABLE ... AUTO_INCREMENT =从MySQL 5.0.3开始的值,但是如果该值小于该列中的当前最大值,则不会发生错误,并且当前序列值不会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM