mysql判斷表是否存在然後批量刪除的操作

2022-09-25 05:54:11 字數 1548 閱讀 8595

1、在網上找了許久都沒有找到判斷表是否存在,程式設計客棧然後模糊刪除的。但是最後找到了乙個曲線救國的方法。

需求:刪除表中字尾是 _待刪除 的表

1、第一步,找到所有的這樣的表,並且拼裝好刪除表的sql

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

from information_schema.tables

where table_name like '%_待刪除';

執行結果

2、第二步:去一一執行刪除語句,有能力的可以去寫乙個指令碼執行什麼的

3、sql解析

3-1 :concat 字串拼接

3-2 :drop table if exists eqwww.cppcns.comui_accp_info__待刪除; --判斷這個表是否存在,如果存在就刪除

補充知識:mysql 判斷表字段是否存在,然後修改

我就廢話不多說了,大家還是直接看**吧~

-- 判斷 vrv_paw_rule 表是否存在 thresholdmin 字段,不存在則新增; 存在則修改字段型別

delimiter ??

drop procedure if exists schema_change??

create procedure schema_change()

begin

if not exists (select * from information_schema.columns where table_schema = database() and table_name = 'vrv_paw_rule' and column_name = 'thresholdmin') then

alter table vrv_paw_rule add column thresholdmin bigint;

程式設計客棧else

alter table vrv_pa程式設計客棧w_rule modify column thresholdmin bigint ;

end if;

end??

delimiter ;

call schema_change();

delimit命令:

就是告訴mysql直譯器,該段命令是否已經結束了,mysql是否可以執行了,即改變輸入結束符。

預設情況下,delimiter是分號「;」。

在命令列客戶端中,如果有一行命令以分號結束,

那麼回車後,mysql將會執行該命令。

但有時候,不希望mys這麼做。因為可能輸入較多的語句,且語句中包含有分號。

預設情況下,不可能等到使用者把這些語句全部輸入完之後,再執行整段語句。

因為mysql一遇到分號,它就要自動執行。

這種情況下,就可以使用delimiter,把delimiter後面換成其它符號,如//或$$。

此時,delimiter作用就是對整個小段語句做乙個簡單的封裝。

此命令多用在定義子程式,觸發程式等mysql自己內嵌小程式中。

本文標題: mysql判斷表是否存在然後批量刪除的操作

本文位址:

mysql判斷表是否存在然後批量刪除

1 在網上找了許久都沒有找到判斷表是否存在,然後模糊刪除的。但是最後找到了乙個曲線救國的方法。1 第一步,找到所有的這樣的表,並且拼裝好刪除表的sql select concat drop table if exists table name,from information schema.tabl...

MySQL判斷表是否存在

有四種方式進行判斷 12 3 4 5 1.show tableslike tb bp d case 2.selecttable namefrominformation schema.tableswhere table schema dbname andtable name tablename 3.如...

python判斷檔案是否存在,是否為空,然後寫入

將每次執行的ks值存入乙個檔案,每次執行時加入檔案,再讀取檔案畫出連續一些天數的ks曲線圖 if os.path.exists ks accumulate.csv if os.path.getsize ks accumulate.csv print 檔案存在且不為空 ks temp.to csv k...