---
title: "批量删除数据库表"
author: "wode66"
date: "2011-07-26T13:34:25Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2011-07-26T13:51:54Z"
id: 1133002
url: "https://suo.iteye.com/blog/1133002"
---

mysql中批量删除以某个前缀开头的表，没有现成的sql，只能写存储过程，定义如下：

```
delimiter $$
create procedure drop_tables_like(pattern varchar(255), db varchar(255))
begin
select @str_sql:=concat('drop table ', group_concat(table_name))
from information_schema.tables
where table_schema=db and table_name like pattern;

prepare stmt from @str_sql;
execute stmt;
drop prepare stmt;
end$$
```

 执行：

```
call drop_tables_like('kw_%', 'db_1')$$
```

 删除存储过程：

```
drop procedure if exists drop_tables_like$$
delimiter ;
```

 

 

参考文章：

  [http://dev.mysql.com/doc/refman/5.0/en/drop-table.html](http://dev.mysql.com/doc/refman/5.0/en/drop-table.html)