Problem with auto-incremented “id” column

前端 未结 6 2085
别跟我提以往
别跟我提以往 2021-01-06 01:26

My db table looks like this pic. http://prntscr.com/22z1n

Recently I\'ve created delete.php page. it works properly but when i deleted 21th user next registered use

相关标签:
6条回答
  • 2021-01-06 02:13

    It's not a good practice to reset auto_increment value, but if you really need to do it, so you can:

    ALTER TABLE mytable AUTO_INCREMENT = 1;
    

    Run this query after every delete. Auto_increment value will not be set to 1, this will set the lowest possible value automatically.

    0 讨论(0)
  • 2021-01-06 02:15

    When you use an autoincrement id column, the value that the next entry will be assigned will not be reduced by deleting an entry. That is not what an autoincrement column is used for. The database engine will always increment that number on a new insert and never decrement that number on a delete.

    0 讨论(0)
  • 2021-01-06 02:16

    Auto increment is a sequence key that's tracked as part of the table. It does not go back when you delete a row.

    0 讨论(0)
  • 2021-01-06 02:18

    A MySQL auto_increment column maintains a number internally, and will always increment it, even after deletions. If you need to fill in an empty space, you have to handle it yourself in PHP, rather than use the auto_increment keyword in the table definition.

    Rolling back to fill in empty row ids can cause all sorts of difficulty if you have foreign key relationships to maintain, and it really isn't advised.

    The auto_increment can be reset using a SQL statement, but this is not advised because it will cause duplicate key errors.

    -- Doing this will cause problems!
    ALTER table AUTO_INCREMENT=12345;
    

    EDIT To enforce your foreign key relationships as described in the comments, you should add to your table definition:

    FOREIGN KEY (friendid) REFERENCES registration_table (id) ON DELETE SET NULL;
    

    Fill in the correct table and column names. Now, when a user is deleted from the registration, their friend association is nulled. If you need to reassociate with a different user, that has to be handled with PHP. mysql_insert_id() is no longer helpful.

    If you need to find the highest numbered id still in the database after deletion to associate with friends, use the following.

    SELECT MAX(id) FROM registration_table;
    
    0 讨论(0)
  • 2021-01-06 02:29

    What you want to do is achievable by adding an extra column to your table called something like user_order. You can then write code to manage inserts and deletions so that this column is always sequential with no gaps.

    This way you avoid the problems you could have messing around with an auto_increment column.

    0 讨论(0)
  • 2021-01-06 02:33

    Easily, no. What you can do (but I don't suggest doing) is making an SQL function to determine the lowest number that isn't currently occupied. Or you can create a table of IDs that were deleted, and get the smallest number from there. Or, and this is the best idea, ignore the gaps and realize the database is fine.

    0 讨论(0)
提交回复
热议问题