How to Reset an MySQL AutoIncrement using a MAX value from another table?

前端 未结 8 674
遥遥无期
遥遥无期 2020-12-08 07:04

I know this won\'t work, tried it in various forms and failed all times. What is the simplest way to achieve the following result?

ALTER TABLE XYZ AUTO_INCRE         


        
8条回答
  •  眼角桃花
    2020-12-08 07:14

    If you really want to do this in MySQL alone, you can just dump the dynamically built alter command to a file on disk and then execute it.

    Like so:

    select concat('ALTER TABLE XYZ AUTO_INCREMENT = ',max(ID)+1,';') as alter_stmt
    into outfile '/tmp/alter_xyz_auto_increment.sql'
    from ABC;
    
    \. /tmp/alter_xyz_auto_increment.sql
    

提交回复
热议问题