MySQL bulk drop table where table like?

前端 未结 5 2022
暖寄归人
暖寄归人 2020-12-12 21:50
DROP TABLE (
SELECT table_name
FROM information_schema.`TABLES`
WHERE table_schema = \'myDatabase\' AND table_name LIKE BINARY \'del%\');

I know th

5条回答
  •  天命终不由人
    2020-12-12 22:06

    A minor improvement to @Devart's answer:

    SET @tables = NULL;
    SELECT GROUP_CONCAT(table_schema, '.`', table_name, '`') INTO @tables FROM
    (select * from
     information_schema.tables 
      WHERE table_schema = 'myDatabase' AND table_name LIKE 'del%'
      LIMIT 10) TT;
    
    SET @tables = CONCAT('DROP TABLE ', @tables);
    select @tables;
    PREPARE stmt1 FROM @tables;
    EXECUTE stmt1;
    DEALLOCATE PREPARE stmt1;
    

    This script should be executed repeatedly until the console's output is NULL

    The changes are:

    1. backtick (`) wrapping the table name (if it contains non standard characters)
    2. added a LIMIT to avoid the truncation issue I commented about
    3. added a "print" (select @tables;) to have some kind of control when to stop executing the script

提交回复
热议问题