MySQL中批量刪除指定字首表的sql語句

2021-07-11 10:44:58 字數 1618 閱讀 2882

想要實現mysql>drop table like "prefix_%"

沒有直接可用的命令,不過可以通過mysql語法來組裝,

[sql]view plain

copy

select concat( 'drop table ', group_concat(table_name) , ';' )   

as statement from information_schema.tables   

where table_schema = 'database_name'

and table_name like

'myprefix_%';  

然後執行該組裝後的命令,比如從slowquery表中刪除掉所有tmp_開頭的表:

[sql]view plain

copy

select concat( 'drop table ', group_concat(table_name) , ';' ) as statement from information_schema.tables where table_schema = 'slowquery'

and table_name like

'tmp_%';  

輸出結果:

[sql]view plain

copy

drop

table tmp_201301,tmp_201302,tmp_201351,tmp_201352;  

執行之即可。

by iefreer

有時候我們在安裝一些cms的時候,這些cms都是帶表字首的方便區分資料,但有時候我們測試完需要刪除的時候又有別的字首表就可以參考下面的方法

複製**

**如下:

select concat( 'drop table ', table_name, ';' )

from information_schema.tables

where table_name like 'dede_%';

"dede"為要刪除的表字首,執行此sql語句後會生成一串sql語句,必須再執行生成的這些sql語句才能真正執行刪除操作

另外乙個就是批量修改表名:

複製**

**如下:

select concat( 'alter table ', table_name, 'rename to ', table_name,';' )

from information_schema.tables

where table_name like 'dede_%';

首先執行此sql語句,會生成如下語句:

複製**

**如下:

alter table de_aaa rename to de_aaa;

alter table de_bbb rename to de_bbb;

在編輯器中將「rename to de」批量改為想設定的表字首,再執行此sql語句即可批量修改表名。

MySQL批量刪除指定字首表

mysql批量刪除指定字首表 select concat drop table table name,from information schema.tables where table name like dede dede 為要刪除的表字首,執行此sql語句後會生成一串sql語句,必須再執行生成...

MySQL批量刪除指定字首表

select concat drop table table name,from information schema.tables where table name like dede dede 為要刪除的表字首,執行此sql語句後會生成一串sql語句,必須再執行生成的這些sql語句才能真正執行刪...

Mysql 批量刪除字首或者字尾表

oracl有drop table like 的用法,但是mysql沒有,可以寫指令碼 不做贅述 也可以組裝sql。注意,我的資料庫名字是test,有個表叫data,然後我準備四個有相同字首的表,批量刪除,準備資料可以用這條sql create table test 1201 select from ...