LoginSignup
1
0

MySQL テーブルごと全部削除

Posted at

以下のクエリで可能

流れ

  1. テーブル名を「, 」区切りで取得
  2. テーブル削除のクエリを作成
  3. 実行
-- 全てのテーブル名を取得する
SET GROUP_CONCAT_MAX_LEN=32768; -- GROUP_CONCATの長さ制限を設定
SELECT GROUP_CONCAT(table_name SEPARATOR ', ') AS tables_to_drop
FROM information_schema.tables
WHERE table_schema = 'データベース名';

-- 取得したテーブル名を使用して全てのテーブルを削除する
SET @tables_to_drop = (
    SELECT GROUP_CONCAT(table_name SEPARATOR ', ')
    FROM information_schema.tables
    WHERE table_schema = 'データベース名'
);

SET @drop_query = CONCAT('DROP TABLE IF EXISTS ', @tables_to_drop, ';');

-- テーブルを削除するクエリを実行する
PREPARE stmt FROM @drop_query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0