AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 1018
Accepted
poelinca
poelinca
Asked: 2011-02-02 23:50:19 +0800 CST2011-02-02 23:50:19 +0800 CST 2011-02-02 23:50:19 +0800 CST

MySQL:以前缀开头的 DROP TABLE

  • 772

示例:我有 30 多个以前缀“dp_”开头的表和大约 12 个以“ex_”开头的表。

问题:如何在一个查询中删除所有以“dp_”开头的表?

mysql
  • 5 5 个回答
  • 21529 Views

5 个回答

  • Voted
  1. Best Answer
    RolandoMySQLDBA
    2011-02-21T21:21:51+08:002011-02-21T21:21:51+08:00

    这是一个接受数据库和前缀字符串作为参数的存储过程:

    DELIMITER $$
    
    DROP PROCEDURE DropTablesWithPrefix $$
    CREATE PROCEDURE DropTablesWithPrefix(db VARCHAR(64),prfx VARCHAR(20))
    StoredProcedure:BEGIN
        DECLARE ndx,maxidx INT;
        DECLARE giventable,SQLSTMT VARCHAR(500);
    
        CREATE TABLE TableZapList
        (
            droptablesql VARCHAR(512),
            idx INT NOT NULL AUTO_INCREMENT PRIMARY KEY
        ) ENGINE=MyISAM;
    
        INSERT INTO TableZapList (droptablesql)
        SELECT CONCAT('DROP TABLE ',table_schema,'.',table_name)
        FROM information_schema.tables
        WHERE table_schema = db AND SUBSTR(table_name,LENGTH(prfx)) = prfx;
        SELECT COUNT(1) INTO maxid FROM TableZapList;
    
        IF maxid = 0 THEN
            LEAVE StoredProcedure;
        END IF;<BR>
    
        SET ndx = 0;
        WHILE ndx < maxid DO
            SET ndx = ndx + 1;
            SELECT droptablesql INTO SQLSTMT FROM TableZapList WHERE idx = ndx;
            SET @sql = SQLSTMT;
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
        END WHILE;<BR>
    
        SELECT droptablesql FROM TableZapList;
        DROP TABLE TableZapList;
    END;
    
    DELIMITER ;
    

    更新 2011-07-12 14:55 EDT

    我只是想到了一种更清洁,简单的方法。无需使用存储过程,只需使用 GROUP_CONCAT 函数来收集所有要 zap 的表。然后组合成一个查询:

    这是一个删除当前数据库中所有以 wp_pol 开头的表的查询:

    SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';')
    FROM information_schema.tables
    WHERE table_schema=database()
    AND table_name like 'wp_pol%';
    

    接下来要做的是将它的结果存储在

    SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';')
    INTO @dropcmd
    FROM information_schema.tables
    WHERE table_schema=database()
    AND table_name like 'wp_pol%';
    

    最后一件事是使用这三 (3) 个命令执行动态 SQL:

    PREPARE s1 FROM @dropcmd;
    EXECUTE s1;
    DEALLOCATE PREPARE s1;
    

    这是在 Windows 中使用 MySQL 5.5.12 的演示:

    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 11
    Server version: 5.5.12 MySQL Community Server (GPL)
    
    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
    
    MySQL (Current test) :: use garbage
    Database changed
    MySQL (Current garbage) :: show tables;
    +------------------------------+
    | Tables_in_garbage            |
    +------------------------------+
    | datas                        |
    | rolando                      |
    | wp_commentmeta               |
    | wp_comments                  |
    | wp_contact_form_7            |
    | wp_links                     |
    | wp_most_read_hits            |
    | wp_options                   |
    | wp_pollsa                    |
    | wp_pollsip                   |
    | wp_pollsq                    |
    | wp_postmeta                  |
    | wp_posts                     |
    | wp_posts_idtracker           |
    | wp_tantan_wordpress_s3_cache |
    | wp_term_relationships        |
    | wp_term_taxonomy             |
    | wp_terms                     |
    | wp_usermeta                  |
    | wp_users                     |
    | wp_w3tc_cdn_queue            |
    +------------------------------+
    21 rows in set (0.00 sec)
    
    MySQL (Current garbage) :: SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';') INTO @dropcmd FROM information_schema.tables WHERE table_schema=database() AND table_name like 'wp_pol%';
    Query OK, 1 row affected (0.00 sec)
    
    MySQL (Current garbage) :: SELECT @dropcmd;
    +--------------------------------------------------------------------+
    | @dropcmd                                                           |
    +--------------------------------------------------------------------+
    | DROP TABLE garbage.wp_pollsa,garbage.wp_pollsip,garbage.wp_pollsq; |
    +--------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    MySQL (Current garbage) :: PREPARE s1 FROM @dropcmd; EXECUTE s1; DEALLOCATE PREPARE s1;
    Query OK, 0 rows affected (0.00 sec)
    Statement prepared
    
    Query OK, 0 rows affected (0.01 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    MySQL (Current garbage) :: show tables;
    +------------------------------+
    | Tables_in_garbage            |
    +------------------------------+
    | datas                        |
    | rolando                      |
    | wp_commentmeta               |
    | wp_comments                  |
    | wp_contact_form_7            |
    | wp_links                     |
    | wp_most_read_hits            |
    | wp_options                   |
    | wp_postmeta                  |
    | wp_posts                     |
    | wp_posts_idtracker           |
    | wp_tantan_wordpress_s3_cache |
    | wp_term_relationships        |
    | wp_term_taxonomy             |
    | wp_terms                     |
    | wp_usermeta                  |
    | wp_users                     |
    | wp_w3tc_cdn_queue            |
    +------------------------------+
    18 rows in set (0.00 sec)
    
    MySQL (Current garbage) ::
    

    试试看 !!!

    • 11
  2. Gaius
    2011-02-03T02:34:21+08:002011-02-03T02:34:21+08:00

    首先,在 Unix 提示符下生成一个脚本来执行此操作:

    $  echo "select concat('drop table ', table_name, ';') from information_schema.tables where table_name like 'prefix_%';" |mysql --user=root --password=blah --batch >drop.sql
    

    将前缀替换为您自己的。该--batch选项会抑制 MySQL 默认执行的精美格式,因此您可以生成可运行的 SQL 脚本。

    查看脚本,如果看起来正常,drop.sql请在mysql>提示符下运行。

    • 8
  3. Marian
    2011-02-03T00:20:20+08:002011-02-03T00:20:20+08:00

    您应该查询系统表以获取这些表名并构建一个字符串以动态执行它。在 SQL Server 中,我会执行以下操作:

    declare @x varchar(max), @enter char(2);
    select @x = '', @enter = char(13)+char(10);
    
    Select @x = @x + 'drop table ' + table_name + ';' + @enter    
    from information_schema.tables    
    where table_name like '%accounting%'
    
    print @x
    execute (@x);
    

    现在你必须在 MySQL 中找到对应的系统表。

    • 4
  4. randomx
    2011-06-17T15:27:48+08:002011-06-17T15:27:48+08:00

    要回答你的问题,不。不在 MySQL 中使用单个命令/查询。你必须链接命令。

    但是,如果你想实现你的目标,这里有一种方法:

    从 bash 脚本类似于:

    #/bin/bash
    TABLES=`mysql -s -e "SELECT CONCAT(TABLE_SCHEMA,'.',TABLE_NAME) AS T FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'dp_%'" -u user -p`
    
    for i in $TABLES; 
    do 
     echo "DROP TABLE $i;" >> drops.sql ; 
    done
    
    cat drops.sql
    

    然后查看 drops.sql 文件。如果一切都好,做一个BACKUP,然后......

    mysql -u username -p -v --show-warnings < drops.sql
    
    • 3
  5. Nestor Gonzalez
    2011-12-09T12:20:15+08:002011-12-09T12:20:15+08:00

    你总是可以使用像navicat这样的数据库管理员,这样的问题就会通过简单的选择和删除来消失。

    • 1

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    您如何显示在 Oracle 数据库上执行的 SQL?

    • 2 个回答
  • Marko Smith

    如何选择每组的第一行?

    • 6 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    我可以查看在 SQL Server 数据库上运行的历史查询吗?

    • 6 个回答
  • Marko Smith

    如何在 PostgreSQL 中使用 currval() 来获取最后插入的 id?

    • 10 个回答
  • Marko Smith

    如何在 Mac OS X 上运行 psql?

    • 11 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Marko Smith

    将数组参数传递给存储过程

    • 12 个回答
  • Martin Hope
    Manuel Leduc PostgreSQL 多列唯一约束和 NULL 值 2011-12-28 01:10:21 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Stuart Blackler 什么时候应该将主键声明为非聚集的? 2011-11-11 13:31:59 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    BrunoLM Guid vs INT - 哪个更好作为主键? 2011-01-05 23:46:34 +0800 CST
  • Martin Hope
    bernd_k 什么时候应该使用唯一约束而不是唯一索引? 2011-01-05 02:32:27 +0800 CST
  • Martin Hope
    Patrick 如何优化大型数据库的 mysqldump? 2011-01-04 13:13:48 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve