Auto_increment values in InnoDB?

前端 未结 5 574
眼角桃花
眼角桃花 2020-12-18 01:00

I\'ve been using InnoDB for a project, and relying on auto_increment. This is not a problem for most of the tables, but for tables with deletion, this might be an issue:

5条回答
  •  独厮守ぢ
    2020-12-18 01:13

    If you have a foreign key constraint, how can you delete a row from table A when table B references that row? That seems like an error to me.

    Regardless, you can avoid the reuse of auto-increment values by resetting the offset when your application starts back up. Query for the maximum in all the tables that reference table A, then alter the table above that maximum, e.g. if the max is 989, use this:

    alter table TableA auto_increment=999;
    

    Also beware that different MySQL engines have different auto-increment behavior. This trick works for InnoDB.

提交回复
热议问题