Delete data from all tables in MYSQL

前端 未结 16 1762
自闭症患者
自闭症患者 2020-12-13 09:17

I have 100 tables, 40,000 rows in each table. I want to go into MySQL and delete all rows from all tables.

...in 1 statement, if p

16条回答
  •  盖世英雄少女心
    2020-12-13 09:53

    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `cleanAllTables`$$
    
    CREATE DEFINER=`root`@`%` PROCEDURE `cleanAllTables`()
    
    BEGIN
    
    DROP Temporary TABLE IF EXISTS AllTables;
    
    Create Temporary Table AllTables (
    
    SELECT @curRow := @curRow + 1 AS row_number
    , table_name 
    FROM INFORMATION_SCHEMA.tables s
    JOIN    (SELECT @curRow := 0
    ) r
    WHERE s.table_schema = 'databasename');
    
    set @countOfAllTables = (select count(*) from AllTables);
    set @c = 1;
    WHILE @c<=@countOfAllTables DO
    
        set @table_name = (select table_name from AllTables where row_number = @c);
        set @stmt = concat( 'Truncate Table ', @table_name);
        Prepare stmt from @stmt;
        Execute stmt;
      SET @c = @c + 1 ;
    END WHILE ;
    
    END$$
    
    DELIMITER ;
    

提交回复
热议问题