Is there a MySQL command to implement something like “drop tables except t1,b2”?

后端 未结 2 1448
轻奢々
轻奢々 2020-12-22 04:08

I want to keep t1,t2 and drop all other tables.

2条回答
  •  猫巷女王i
    2020-12-22 04:26

    You can use information_schema to find table names, and even format the results as a bunch of DROP statements.

    SELECT CONCAT('DROP TABLE ', TABLE_NAME, '; ')
      FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name NOT IN ('foo', 'bar', 'baz');
    

    (The DATABASE() function returns the currently use'd database.)

    Using PREPARE and EXECUTE, you could even avoid copy & paste, and (in MySQL 5.0.13 and later) write a stored procedure to do this.

提交回复
热议问题