Updating AUTO_INCREMENT value of all tables in a MySQL database

前端 未结 8 1654
旧巷少年郎
旧巷少年郎 2020-12-02 22:52

It is possbile set/reset the AUTO_INCREMENT value of a MySQL table via

ALTER TABLE some_table AUTO_INCREMENT = 1000

However I need

8条回答
  •  Happy的楠姐
    2020-12-02 23:30

    I have written below procedure change the database name and execute the procedure

    CREATE DEFINER=`root`@`localhost` PROCEDURE `setAutoIncrement`()
    BEGIN
    DECLARE done int default false;
        DECLARE table_name CHAR(255);
    DECLARE cur1 cursor for SELECT t.table_name FROM INFORMATION_SCHEMA.TABLES t 
            WHERE t.table_schema = "buzzer_verifone";
    
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
        open cur1;
    
        myloop: loop
            fetch cur1 into table_name;
            if done then
                leave myloop;
            end if;
            set @sql = CONCAT('ALTER TABLE ',table_name, ' AUTO_INCREMENT = 1');
            prepare stmt from @sql;
            execute stmt;
            drop prepare stmt;
        end loop;
    
        close cur1;
    END
    

    Execute the procedure above using below line

    Call setAutoIncrement();
    

提交回复
热议问题