Auto_increment values in InnoDB?

前端 未结 5 575
眼角桃花
眼角桃花 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:12

    I checked. alter table TableA auto_increment=1; does NOT work. And the reason I found in two documents

    http://docs.oracle.com/cd/E17952_01/refman-5.1-en/innodb-auto-increment-handling.html InnoDB uses the following algorithm to initialize the auto-increment counter for a table t that contains an AUTO_INCREMENT column named ai_col: After a server startup, for the first insert into a table t, InnoDB executes the equivalent of this statement: SELECT MAX(ai_col) FROM t FOR UPDATE; InnoDB increments the value retrieved by the statement and assigns it to the column and to the auto-increment counter for the table. By default, the value is incremented by one. This default can be overridden by the auto_increment_increment configuration setting.

    and http://docs.oracle.com/cd/E17952_01/refman-5.1-en/alter-table.html

    You cannot reset the counter to a value less than or equal to any that have already been used.

    This is the reason why alter table will not work. I think that only option is to wipe out data and rewrite it in a new table with new id. In my case table was logfile , so I just did:

    RENAME TABLE SystemEvents To SystemEvents_old; CREATE TABLE SystemEvents LIKE SystemEvents_old;

提交回复
热议问题