Rol*_*DBA 11
这是一个接受数据库和前缀字符串作为参数的存储过程:
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 ;
Run Code Online (Sandbox Code Playgroud)
更新 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%';
Run Code Online (Sandbox Code Playgroud)
接下来要做的是将结果存储在
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%';
Run Code Online (Sandbox Code Playgroud)
最后一件事是使用这三 (3) 个命令执行动态 SQL:
PREPARE s1 FROM @dropcmd;
EXECUTE s1;
DEALLOCATE PREPARE s1;
Run Code Online (Sandbox Code Playgroud)
这是在 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) ::
Run Code Online (Sandbox Code Playgroud)
试一试 !!!
首先,在 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
Run Code Online (Sandbox Code Playgroud)
用您自己的前缀替换前缀。该--batch选项禁止 MySQL 默认执行的花哨格式,因此您可以生成可运行的 SQL 脚本。
查看脚本,如果它看起来没问题,请drop.sql在mysql>提示符下运行。
| 归档时间: |
|
| 查看次数: |
20957 次 |
| 最近记录: |