remove gaps in auto increment

前端 未结 2 1432
小鲜肉
小鲜肉 2021-01-14 13:50

Say I have a MySQL table with an auto incrementing id field, then I insert 3 rows. Then, I delete the second row. Now the id\'s of the table go 1,3. Can I get MySQL to corre

2条回答
  •  攒了一身酷
    2021-01-14 14:17

    Quoting from The Access Ten Commandments (and it can be extensible to other RDBMS: "Thou shalt not use Autonumber (or Auto Incremental) if the field is meant to have meaning for thy users".

    The only alternative I can think of (using only MySQL) is to:

    1. Create a trigger that adds the row number to a column (not the primary key)
    2. Create a procedure to delete rows and update the row number (I couldn't make this work with triggers, sorry)

    Example:

    create table tbl_dummy(
        id int unsigned not null auto_increment primary key,
        row_number int unsigned not null default 0,
        some_value varchar(100)
    );
    
    delimiter $$
    
    -- This trigger will add the correct row number for each record inserted 
    -- to the table, regardless of the value of the primary key    
    create trigger add_row_number before insert on tbl_dummy
    for each row
    begin
        declare n int unsigned default 0;
        set n = (select count(*) from tbl_dummy);
        set NEW.row_number = n+1;
    end $$
    
    -- This procedure will update the row numbers for the records stored
    -- after the id of the soon-to-be-deleted record, and then deletes it.
    create procedure delete_row_from_dummy(row_id int unsigned)
    begin
        if (select exists (select * from tbl_dummy where id = row_id)) then
            update tbl_dummy set row_number = row_number - 1 where id > row_id;
            delete from tbl_dummy where id = row_id;
        end if;
    end $$
    
    delimiter ;
    

    Notice that you'll be forced to delete the records one by one, and you'll be forced to get the correct primary key value of the record you want to delete.

    Hope this helps

提交回复
热议问题